JavaScript: NaN


NaN is a value represents not a number.

NaN is a top level property and is not associated with any object.

NaN is never equal to any other number, including NaN itself. It is not possible to check whether a value is a number by comparing it with NaN using equal to operator. You have to use the isNaN function instead.

Methods like Number constructor, parseFloat, and parseInt return NaN if the argument passed to them are not numbers.

NaN property can be used to refer an error for a function, provided the function returns a valid number.



Example of JavaScript NAN - when math function fails

JavaScript Code:


View example of JavaScript NAN - when math function fails

Live demo:

See the Pen nan-property-1 by w3resource (@w3resource) on CodePen.

JavaScript Code:


View Example JavaScript NAN - when a function tries to read a number fails

Live demo:

See the Pen nan-property-2 by w3resource (@w3resource) on CodePen.


Since Equality operator (i.e. == and ===) cannot be used to test a value against NaN, If you wish to perform such a test, use isNAN instead of NAN.

See the Pen javascript-common-editor by w3resource (@w3resource) on CodePen.

Previous: JavaScript: parseFloat() function
Next: JavaScript: Number functionJavaScript: Number function

JavaScript: Tips of the Day

Returns all unique values of an array, based on a provided comparator function


const tips_uniqueElementsBy = (arr, fn) =>
  arr.reduce((acc, v) => {
    if (!acc.some(x => fn(v, x))) acc.push(v);
    return acc;
  }, []);

    { id: 0, value: 'p' },
    { id: 1, value: 'q' },
    { id: 2, value: 'r' },
    { id: 1, value: 's' },
    { id: 0, value: 't' }
  (a, b) => a.id == b.id


[[object Object] {
  id: 0,
  value: "p"
}, [object Object] {
  id: 1,
  value: "q"
}, [object Object] {
  id: 2,
  value: "r"