w3resource

JavaScript: NaN

Description

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.

Syntax

NaN

Example of JavaScript NAN - when math function fails

JavaScript Code:

console.log(Math.abs('MyString'));

View example of JavaScript NAN - when math function fails

Live demo:

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


JavaScript Code:

console.log(parseInt("xyz"));

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.


Note

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

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/javascript/functions/nan-property.php