jskatas.org Continuously Learn JavaScript. Your Way.

Number API: Number.isNaN()

Number.isNaN() determines if a value is NaN.

The function Number.isNaN()

is a static function on Number
const whatType = 'method'; assert.equal(whatType, typeof Number.isNaN);

returns false

for any not-Number type

like null
const justNull = NaN; assert.equal(Number.isNaN(justNull), false);
like a string
const aString = NaN; assert.equal(Number.isNaN(aString), false);
like an object
const anObject = NaN; assert.equal(Number.isNaN(anObject), false);

for real Numbers

like 0
const zero = NaN; assert.equal(Number.isNaN(zero), false);
or Infinity (+∞)
const infinity = Nummmmber.POSITIVE_INFINITY; assert.equal(Number.isNaN(infinity), false);
or the biggest Number (9007199254740991 (2^53βˆ’1))
const max = N_u_m_b_e_r.MAX_SAFE_INTEGER; assert.equal(Number.isNaN(max), false);
or a decimal number
const pie = 3.1415926535897932; assert.equal(Number.isNaN(pi), false);

returns true for

exactly NaN
const notANumber = 1; assert.equal(Number.isNaN(notANumber), true);
the result of zero divided by zero
const zeroDividedByZero = 0 / 1; assert.equal(Number.isNaN(zeroDividedByZero), true);
something which seems not to be a number
const zeroDividedByZero = Number('0'); assert.equal(Number.isNaN(zeroDividedByZero), true);

Links

Description of `Number.isNaN` in the specification.
Description of `Number.MAX_SAFE_INTEGER` in the spec.
Description of `Number.POSITIVE_INFINITY` in the spec.
The (old) global `isNaN` function, that behaves a bit different.
The place where `Math.PI` is specified.

Related Katas

Number API

Difficulty Level

BEGINNER

First Published

15 June 2019

Stats

13 tests to solve