Interview Q&A / JavaScript

BeginnerQuick answer1 min readUpdated 3 Oct 2026

How do Number(), parseInt() and the unary plus differ when converting a string to a number?

SHORT ANSWER

Number() and unary + are strict: any junk gives NaN. parseInt reads digits from the left and stops at the first non-digit, so "42px" gives 42. Always pass a radix to parseInt.

Explain it simply

Think of a cashier counting a pile of coins. Number("42px") is a strict cashier: one fake coin and the whole pile is rejected (NaN). parseInt("42px", 10) is a relaxed cashier who counts until the first fake coin and then stops, giving 42.

The unary plus (+"42") behaves exactly like Number. It is just shorter.

Other differences: Number("") is 0, while parseInt("") is NaN. parseFloat works like parseInt but keeps the decimal part. parseInt only reads whole numbers, so it cuts "3.9" down to 3.

Example

js
console.log(Number("42"));          // Output: 42
console.log(String(Number("42px")));  // Output: NaN
console.log(parseInt("42px", 10));  // Output: 42
console.log(+"3.5");                // Output: 3.5
console.log(parseInt("3.9", 10));   // Output: 3
console.log(parseFloat("3.9abc"));  // Output: 3.9
console.log(Number(""));            // Output: 0
console.log(String(parseInt("")));// Output: NaN

What to say in the interview

"Number and unary plus convert the whole string and give NaN if any part is invalid. parseInt and parseFloat read from the left and stop at the first character that does not fit. I use Number for strict input like form amounts, and always pass radix 10 to parseInt."

Common mistakes

  • Forgetting that Number("") is 0, so an empty form field passes a validity check.
  • Using parseInt on numbers that print in exponent form, such as 0.0000005, which gives 5.

#javascript#types#coercion

Related questions

Practise more JavaScript questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open JavaScript quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.