Adriel Werlich
Adriel Werlich

Reputation: 2331

Number.parseInt of a not alphanumeric string gives a typeof 'number'

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

Upvotes: 1

Views: 422

Answers (2)

Clarity
Clarity

Reputation: 10873

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.

Upvotes: 9

user11547066
user11547066

Reputation:

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.

Upvotes: 1

Related Questions