Saturday, August 30, 2025
HomeLanguagesJavascriptJavaScript Number NaN Property

JavaScript Number NaN Property

In JavaScript, NaN stands for Not a Number. It represents a value that is not a valid number. It can be used to check whether a number entered is a valid number or not a number. To assign a variable to NaN value, we can use one of the two following ways.

Syntax:

let a = NaN
    // OR
let a = Number.NaN

Example 1: In this example, we will use JavaScript Number NaN Property.

javascript




let monthNumber = 14;
 
if (monthNumber < 1 || monthNumber > 12) {
 
    // Assigning monthNumber NaN as
    // month number is not valid
    monthNumber = Number.NaN;
 
    console.log("Month number should be"
                + " between 1 and 12");
}
else {
    console.log(monthNumber);
}


Output:

Month number should be between 1 and 12

Example 2: Whenever we try to parse a string or “undefined” to an int, it returns NaN. 

javascript




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


Output:

Nan

Example 3: Whenever we try to find square root of a negative number using Math.sqrt function, it returns NaN.

javascript




console.log(Math.sqrt(-1));


Output:

NaN

Example 4: Whenever we try to make on operation on NaN, it returns NaN.

javascript




console.log(5 + NaN);


Output:

NaN

Example 5: Any indeterminate form also returns NaN.

javascript




console.log(0 * Infinity)


Output:

NaN

Example 6: Any operation other than addition on a string also results in NaN.

javascript




console.log("hi"/5)


Output:

NaN

Supported Browser: 

  • Chrome 1 and above
  • Edge 12 and above
  • Firefox 1 and above
  • Internet Explorer 4 and above
  • Opera 3 and above
  • Safari 1 and above

We have a Cheat Sheet on Javascript Numbers where we covered all the important topics of Javascript to check those please go through JavaScript Number Complete Reference.

RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32249 POSTS0 COMMENTS
Milvus
80 POSTS0 COMMENTS
Nango Kala
6617 POSTS0 COMMENTS
Nicole Veronica
11792 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11838 POSTS0 COMMENTS
Shaida Kate Naidoo
6731 POSTS0 COMMENTS
Ted Musemwa
7012 POSTS0 COMMENTS
Thapelo Manthata
6689 POSTS0 COMMENTS
Umr Jansen
6701 POSTS0 COMMENTS