The NaN Acronym

By: Dusty Arlia
Published on October 31, 2012
Last Updated on Thursday, July 16, 2015 at 9:01 PM
Total Updates: 5

The NaN acronym stands for "not a number". You will see variables with this value if their data type is a number, but they were assigned a value which is not a number.

NaN is a predefined global variable with a value of NaN. This variable is incorrectly read/writeable in ECMAScript 3, but was fixed to read-only in ECMAScript 5. Here are some of the ways your expression can evaluate to NaN:

  • Zero divided by zero
  • Infinity divided by Infinity
  • Square root of a negative number
  • Using an arithmetic operator on non-numeric operands that cannot be converted to numbers

The variable NaN evaluates to NaN. In ECMAScript 3, it is read/writeable. In ECMAScript 5, it is read-only. The Number object has read-only properties that corrects this. The Number.NaN property evaluates to NaN, which is read-only in both ECMAScript 3 and 5.

There are a couple of ways to test a variable to see if it evaluates to NaN. The NaN value does not equal any other value including the NaN value. alert(NaN == NaN) alerts the message false. So if you had an example_nan_variable, this wouldn't work: example_nan_variable == NaN. Instead, try this: example_nan_variable != example_nan_variable. If this returns true, then you know your variable equals NaN. You can also try using the isNaN() function. The isNaN() function returns true if its argument is NaN or if it is a non-numeric value.