11/23/21, 5:13 PM An Essential Guide to JavaScript NaN
JavaScript NaN
Summary: in this tutorial, you’ll learn about the
JavasScript NaN , how to check if a value is NaN , and
how to handle NaN effectively.
Introduction to JavaScript NaN
JavaScript has the number
([Link] type that
allows you to represent numbers including integer and
floating-point numbers. And JavaScript number has a
special value called NaN , which stands for Not-a–
Number.
The NaN is a property of the global object
([Link] .
[Link] 1/8
11/23/21, 5:13 PM An Essential Guide to JavaScript NaN
The global object is the window
([Link]
object in web browsers:
[Link]
And the global object in [Link].
[Link]
The NaN has the type number as shown in the
following code:
[Link](typeof NaN); // number
Checking if a value is NaN
JavaScript provides you with the global function
isNaN() that returns true if its argument is NaN :
[Link] 2/8
11/23/21, 5:13 PM An Essential Guide to JavaScript NaN
isNaN(valueToCheck)
For example:
const result = 100 + 0 / 0;
[Link](isNaN(result)); // true
Why use NaN
JavaScript uses NaN as the result of a failed operation
on numbers including:
Parsing numbers
Using undefined
([Link]
undefined/) as an operand
Using NaN as an operand
Using indeterminate forms
Passing invalid arguments to a math function
[Link] 3/8
11/23/21, 5:13 PM An Essential Guide to JavaScript NaN
Operations return NaN
1) Parsing numbers
In JavaScript, you can convert a numeric string
([Link] to a
number. For example:
const input = '100';
const num = parseInt(input);
[Link](num); // 100
If JavaScript cannot convert a string to a number, it
returns NaN . In this case, NaN indicates that the
parsing has failed. For example:
const input = 'X100';
const num = parseInt(input);
[Link](num); // NaN
[Link] 4/8
11/23/21, 5:13 PM An Essential Guide to JavaScript NaN
It’s good practice to verify the parsing result using the
isNaN function:
const input = 'X100';
const num = parseInt(input);
// set num to 0 if parsing failed
if (isNaN(num)) {
num = 0;
}
[Link](num); // 0
In this example, the parsing has failed therefore it
returned NaN . The condition isNaN(num) is true so
that the number is assigned to 0 .
2) Use undefined as an operand
[Link] 5/8
11/23/21, 5:13 PM An Essential Guide to JavaScript NaN
An expression that uses undefined as an operand
returns NaN . For example:
[Link](undefined * 2); // NaN
In practice, you’ll deal with undefined quite often. For
example, when you access a non-existing property of an
HTML element and use it in a calculation.
It’s good practice to avoid using undefined in
calculation.
3) Using NaN as an operand
When an expression has the NaN, it always returns NaN.
For example:
const result = 10 + 1 / NaN;
[Link](result); // NaN
[Link] 6/8
11/23/21, 5:13 PM An Essential Guide to JavaScript NaN
4) Using indeterminate forms
When an arithmetical operation is in the indeterminate
form, it returns NaN. For example:
const result = 10 + 0 / 0;
[Link](result); // NaN
5) Using invalid arguments of math
functions
When a math function receives an invalid argument, it
returns NaN. For example:
const result = [Link](-1);
[Link](result); // NaN
In this example, the [Link]() that returns the
square root of a number. Since it only accepts a non-
negative number, passing -1 results in NaN .
[Link] 7/8
11/23/21, 5:13 PM An Essential Guide to JavaScript NaN
Summary
NaN stands for Not-a-Number. It is a special value
of the NaN property of the global object.
NaN indicates a failed operation on numbers.
Use isNaN() function to check if a value is NaN .
[Link] 8/8