ES6 - Number.NaN



Unquoted literal constant NaN is a special value representing Not-a-Number. Since NaN always compares unequal to any number, including NaN, it is usually used to indicate an error condition for a function that should return a valid number.

Syntax

var val = Number.NaN;

Example

var dayOfMonth = 50;
if (dayOfMonth < 1 || dayOfMonth > 31) {
   dayOfMonth = Number.NaN
   console.log("Day of Month must be between 1 and 31.")
} else {
   console.log("day of month "+dayOfMonth)
}

The following output is displayed on successful execution of the above code.

Day of Month must be between 1 and 31.
Advertisements