728x90

✨ 숫자(number)를 문자열(string)로 바꿔주는 helper method

(e.g., num.toString()) 왼쪽처럼 num 변수에 숫자가 선언되어 있을 때, 문자열로 변환하여 다른 변수에 저장할 수 있다.
Rever Int(숫자 거꾸로 바꾸기) Algorithm에서 유용하게 사용했다.

💻Example Code
✅ String와 Number를 비교하는 코드로 작성한다.

const num = 31;
const numToString = num.toString();

console.log( num );
console.log( numToString );
console.log( typeof num );
console.log( typeof numToString );

실행 결과(Number, String 순서) - VS Code Node.js 기준

😋 Reverse Int(숫자 거꾸로 바꾸기) Algorithm을 다룰 때, 처음에 받은 case test를 toString()으로 변환 후 문자를 다룬다.
즉, 해당 number를 string으로 바꾼 후 index를 직접 특정하여 다루고 싶을 때 유용하게 사용할 수 있다.

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

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

Array.prototype.join()  (0) 2019.12.24
Array.prototype.reverse()  (0) 2019.12.24
Global.parseInt()  (0) 2019.12.24
Math.sign()  (0) 2019.12.24
Array.prototype.slice()  (0) 2019.12.23

+ Recent posts