728x90

✨ 문자열로 바꾸고싶을 때, toString() 대신에 사용하는 new String();
(e.g., const numToStr = new String(num)) 왼쪽처럼 선언하여 Number를 String으로 사용한다.

💻Example Code

const num = 77;
const numToStr = new String(num);

console.log( num );
console.log( numToStr );
console.log( numToStr.split('') ); // 활용 예시

실행 결과(Number 77, String 77, split된 77)

😋 [String: '77']의 정확한 의미는 모르겠지만, 일반 String처럼 사용 가능하다. 아마 Object가 생성되는 것 같다.
(조금 더 학습할 필요성이 있다)

👉 자세한 내용은 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String

 

String

The String global object is a constructor for strings or a sequence of characters.

developer.mozilla.org

 

'JavaScript > Built-in Method etc.' 카테고리의 다른 글

String.prototype.match()  (0) 2019.12.31
String.charCodeAt()  (0) 2019.12.31
new Array()  (0) 2019.12.29
Array.prototype.메소드() 만들기  (0) 2019.12.29
Array.prototype.fill()  (0) 2019.12.29
728x90

✨ 원하는 개수만큼 array를 선언하는 방법
(e.g., const arr = new Array(100)) 왼쪽처럼 선언하면 간단하게 100개의 array를 생성할 수 있다.

💻Example Code

const arr = new Array(100);

console.log( arr );
console.log( arr.length);

실행 결과(100개의 비어있는 Array 값, Array의 길이는 100)

😋 가끔 큰 index의 Array를 다룰 때, undefined보다는 하나의 값을 전체적으로 지정해주고 싶을 때가 있다. 그때 사용하면 된다.
백준 4673 셀프 넘버(Self Number)에서 유용하게 사용했다.

👉 자세한 내용은 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

 

Array

The JavaScript Array class is a global object that is used in the construction of arrays; which are high-level, list-like objects.

developer.mozilla.org

 

'JavaScript > Built-in Method etc.' 카테고리의 다른 글

String.charCodeAt()  (0) 2019.12.31
new String()  (0) 2019.12.29
Array.prototype.메소드() 만들기  (0) 2019.12.29
Array.prototype.fill()  (0) 2019.12.29
String.prototype.replace()  (0) 2019.12.29
728x90

✨ Array의 모든 index에 값을 한 번에 채워주거나, 원하는 구간만큼 값을 채워줄 수 있는 helper method

(e.g., arr.fill(false) or arr.fill(0, 2, 4)) 왼쪽처럼 Array의 모든 값을 false로 채우거나, index 2부터 3까지 0을 채워줄 수 있다.
(Array.prototype.slice()와 마찬가지로 구간을 지정할 때, end-1까지 진행된다는 점을 명심하자)

💻Example Code

const arr = [ 1, 2, 3, 4, 5, 6 ];

console.log( arr.fill(0, 1, 3) );

실행 결과(index 1부터 3까지 지정했지만, 2까지 변한 것을 볼 수 있다)

😋 매우 큰 Array의 값을 한 번에 초기화 해줄 때, 또는 원하는 index 구간만큼 값을 바꿔줄 때 매우 유용하다.
백준 4673번 셀프 넘버(Self Number)문제를 풀 때 사용했다.

👉 자세한 내용은 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill

 

Array.prototype.fill()

The fill() method changes all elements in an array to a static value, from a start index (default 0) to an end index (default array.length). It returns the modified array.

developer.mozilla.org

 

'JavaScript > Built-in Method etc.' 카테고리의 다른 글

new Array()  (0) 2019.12.29
Array.prototype.메소드() 만들기  (0) 2019.12.29
String.prototype.replace()  (0) 2019.12.29
Object.keys() & Object.values()  (0) 2019.12.29
Array.prototype.sort()  (0) 2019.12.28

+ Recent posts