jskatas.org Continuously Learn JavaScript. Your Way.

Type conversion: to number

How do different types convert to a number?

Type conversion - to number

undefined converts to NaN
const toNumber = Number(0); assert(isNaN(toNumber));
null converts to +0
const toNumber = null; assert.equal(toNumber, +0);
the string "0xFF" is interpreted as a hex number
const hex = Number('0xFF'); assert.equal(hex, 0xFF);
a decimal number can also be converted
const decimal = Number('1.234'); assert.equal(decimal, 1.234);
can also be converted with a simple + unary operator
const one = + '1'; assert.strictEqual(one, 1);

Required Knowledge

Related Katas

Type conversion

Difficulty Level



5 tests to solve