1

this is what´s happening

typeof Number.parseInt('processed') prints 'number'.

enter image description here


But if Number.parseInt('processed') gives NaN.

enter image description here

enter image description here

Clarity
  • 10,730
  • 2
  • 25
  • 35
Adriel Werlich
  • 1,982
  • 5
  • 15
  • 30
  • 1
    If you want to check the result was parsed successfully, use `isNaN(result)` (`true` = didn't parse anything from the string). See also [my answer here](https://stackoverflow.com/questions/28994839/why-does-string-to-number-comparison-work-in-javascript/28994875#28994875) listing the various ways to convert from string to number and their various pitfalls/idiosyncrasies. – T.J. Crowder Aug 18 '19 at 16:23
  • 1
    @T.J.Crowder... thanks... isNaN works for the use case here... – Adriel Werlich Aug 18 '19 at 16:41

2 Answers2

9

Number.parseInt('string') returns NaN, which has a type of number.

You can verify it in the browser console:

typeof NaN === 'number'

true

Here's a handy guide on how to test against NaN.

Clarity
  • 10,730
  • 2
  • 25
  • 35
1

NaN is a value representing Not-A-Number.

Steps:

  1. Number.parseInt('processed') is NaN

  2. typeof NaN is a number.

Why?

The ECMAScript standard states that Numbers should be IEEE-754 floating point data. This includes Infinity, -Infinity, and also NaN.

Other tricky results with NaN:

NaN < 1;    // false
NaN > 1;    // false
NaN == NaN; // false

Testing against NaN:

Use Number.isNaN() or isNaN() to most clearly determine whether a value is NaN.