Trivia Questions2 min readPremium

Convert a String to a Number in JavaScript

Use Number(str) when the whole string must be a valid number, and parseInt(str, 10) or parseFloat(str) when you want the number at the start of a string like "12px". The unary plus (+str) behaves exactly like Number(str).

1. Results side by side

Input Number(x) parseInt(x, 10) parseFloat(x)
"42" 42 42 42
" 42 " 42 42 42
"3.14abc" NaN 3 3.14
"12px" NaN 12 12
"" 0 NaN NaN
"1e3" 1000 1 1000
"0x1f" 31 0 0
"1,000" NaN 1 1
"abc" NaN NaN NaN
null 0 NaN NaN
undefined NaN NaN NaN

2. Number() and unary plus: all or nothing

Number('42')    // 42
+'42'           // 42
Number('42px')  // NaN: any invalid character fails the whole string
Number('')      // 0  ← the one to remember
Number(' ')     // 0

Strict about content, but an empty or whitespace-only string becomes 0. Check for empty input first if 0 would be wrong.

3. parseInt() and parseFloat(): read until it stops making sense

parseInt('12px', 10)    // 12
parseFloat('3.14abc')   // 3.14
parseInt('3.99', 10)    // 3: truncates, doesn't round
parseInt('abc', 10)     // NaN: nothing usable at the start

Always pass the radix. Without it, a string starting with 0x is read as hex:

parseInt('0x1f')      // 31
parseInt('0x1f', 10)  // 0
parseInt('101', 2)    // 5

4. Check the result

function toNumber(input) {
  if (typeof input !== 'string' || input.trim() === '') return null;
  const n = Number(input);
  return Number.isFinite(n) ? n : null;
}

toNumber('42')    // 42
toNumber('')      // null
toNumber('12px')  // null
toNumber('1e999') // null (Infinity)

Use Number.isNaN and Number.isFinite, not the global isNaN, which converts its argument first.

5. The map(parseInt) trap

['1', '2', '3'].map(parseInt) // [1, NaN, NaN]
['1', '2', '3'].map(Number)   // [1, 2, 3]

map passes (value, index), so parseInt receives the index as the radix: parseInt('2', 1) and parseInt('3', 2) are both NaN.

6. Very large integers

Number('9007199254740993')  // 9007199254740992: precision lost
BigInt('9007199254740993')  // 9007199254740993n

Above Number.MAX_SAFE_INTEGER (2^53 − 1), use BigInt, or keep IDs as strings.

7. Formatted input

Nothing built in parses "1,000.50" or "1.000,50". Strip what you know is formatting, then convert:

Number('1,000.50'.replaceAll(',', '')) // 1000.5

For user-entered money or locale-specific formats, validate the pattern first.

Choosing a method

You have Use
A clean numeric string from an API or <input type="number"> Number(str)
A value with units ("12px", "50%") parseFloat(str)
An integer in another base parseInt(str, radix)
An integer too big for a double BigInt(str)
Untrusted input Number + empty check + Number.isFinite

Follow-up questions to expect

  • “Difference between Number('') and parseInt('')?” 0 vs NaN.
  • “How do you get an integer from '7.9'?” Math.trunc(Number('7.9')) for 7, Math.round for 8. parseInt also gives 7 but hides bad input.
  • “What does +'7' + +'3' return?” 10. Without the unary pluses it would be '73'. See type coercion.