본문 바로가기
✘✘✘ Javascript

[javascript] how to parse string to number, Number, parseInt, +

by PrettyLog 2022. 7. 12.

Number() converts the type whereas parseInt parses the value of input.

Number('123');
parseInt('123', 10);
+'123';

Parseing string with non-digit character

Number('123 asf'); // NaN
parseInt('123 asf'); // 123

As you see, parseInt will parse up to the first non-digit character. On the other hand, Number will try to convert the entire string.

binary, decimal

parseInt('0101'); // 101
parseInt('0101', 10); // 101
parseInt('0101', 2); // 5

Number('0101'); // 101

Number() vs parseInt() - HTML DOM

댓글