Numbers
Convert Strings to Number
parseInt('123', 10); // 10 is the base. In old browsers, strings start with 0 will be converted in octal (base 8).
// parseFloat() always uses base 10
+ '42'; // 42
+ '010'; // 10
+ '0x10'; // 16. 0x is 16-based.
parseInt('123abc', 10); // 123
+ '123abc'; // NaN
// Number(str) is the same as + str
// for +, if both perands are numbers, add them. otherwise, convert to strings and concat.
+'3' + (+'4') // 7NaN: Not a number
typeof NaN; // number
NaN === NaN; // false
NaN !== NaN; // true
isNaN(NaN); // true
isNaN("Hello"); // true. Try to convert "Hello" to a number and results in NaN
isFinite(NaN); // false
isFinite(-Infinity); // falseScientific Number Literals
Interesting Arithmetics
Last updated