Javascript Tutorial
Javascript Tutorial Introduction to Javascript JavaScript Code Editors & IDE JavaScript Hello World Example Javascript Syntax and Rules syntax_rules javascript_identifiers JavaScript Keywords & Reserved Words javascript_variables JavaScript Const JavaScript let vs var vs const Data Types in JavaScript JavaScript String Template Literals & String interpolation in JavaScript Tagged Templates in JavaScript String to Number in JavaScript Number Data Type in JavaScript NaN in JavaScript JavaScript Number Min & Max & Safe Values JavaScript EPSILON & Floating point precision Infinity in JavaScript JavaScript Bigint BigInt Vs Number in JavaScript Boolean Data Type in JavaScript Undefined in JavaScript Null in JavaScript Null vs Undefined in JavaScript JavaScript Operators Arithmetic Operators in JavaScript Unary plus & minus operators in JavaScript Increment & Decrement Operators in JavaScript Comparison or Relational operators in JavaScript Strict Equality (==) Loose Equality (===) in JavaScript Ternary Conditional Operator in JavaScript Logical Operators in JavaScript Bitwise Operators in JavaScript Assignment Operators in JavaScript Nullish Coalescing Operator in JavaScript Comma Operator in JavaScript Typeof JavaScript Operator Precedence in JavaScript JavaScript if, else & nested if statement Switch Statement in JavaScript While & Do While Loops in JavaScript For Loop in JavaScript Break statement in JavaScript Continue Statement in JavaScript Arrays in JavaScript Array Constructor in Javascript Sparse Array Vs Dense Array in JavaScript How to merge Arrays in JavaScript Array Methods in JavaScript Functions in JavaScript Function Parameters & Arguments in JavaScript JavaScript Default Parameters Pass by Value and Pass by Reference in Javascript Function Expression in Javascript Nested Functions in JavaScript Immediately-invoked Function Expressions (IIFE) JavaScript Callback Functions Arrow Functions in JavaScript Arguments Object In JavaScript Rest Parameters in JavaScript Objects in Javascript Create Objects in JavaScript JavaScript Object Properties Computed Property Names in JavaScript Object Literal in JavaScript Constructor Function & New Operator in JavaScript Delete Operator in JavaScript hasOwnProperty in JavaScript Using Getters and Setters in Javascript DefineProperty in JavaScript JavaScript Property Descriptors Enumerable, Writable & Configurable Object Destructuring in JavaScript Variable Scope in JavaScript Hoisting in JavaScript Lexical Scope & Closures in JavaScript This in JavaScript Global Object, Window & Globalthis in JavaScript Call function in Javascript Prototype In Javascript Prototype Inheritance in JavaScript Instanceof Operator in JavaScript Spread Operator in JavaScript

NaN in JavaScript

NaN in JavaScript stands for Not a Number. It is the result of numerical operations, where result is not a number. It is represented by a special property of the global object with the name NaN. NaN is also property of the number object and can refer it using the Number.NaN. The typeofNaN is primitive number. We can check whether a value isNaN by using the isNaN function or by Number.IsNan method.

NaN

The number data type represents all numbers in JavaScript. It also contains three special values i.e. Not A Number (NaN), positive infinity and negative infinity.

JavaScript does not throw error when we perform a invalid arithmetic operations like dividing by zero. Instead it returns a special value NaN.

NaN is represented by a Literal NaN, which is a property of the global object. NaN is also property of the number object and can refer it using the Number.NaN

                            
console.log(NaN)                       //NaN
console.log(Number.NaN)                //NaN
                            
                            
                        

NaN is a specal value in the number data type. Hence type of NaN is number.

                            
console.log(typeof(NaN));              //number 
                            
                        
                            
                        

NaN can happen in several ways..

  • Converting an Invalid value to a number
  • Dividing Zero by Zero
  • Operations involving infinity
  • All Operations involving NaN

Converting an Invalid value to a number

In this following case, we are trying to convert a string to a number. Since “test” cannot be converted to a number it results in a NaN.

                            
let num = Number("test");
console.log(num);
 
 
***output***
NaN 
                            
                        
                            
                        

Or using an invalid value in any function that expects a number. For Example Math.round or parseInt methods

Converting Undefined into a number

                            
let v=undefined;
console.log(parseInt(v));       //NaN
                            
                        
                            
                        
                            
let n = Number(undefined)
console.log(n)
                            
                        
                            
                        

Dividing Zero by Zero

Example

                            
let num1 = 0/0;
console.log(num1)
console.log(typeof(num1))
 
 
**output**
NaN
number
        
                        
                            
                        

Operations involving infinity

Multiply an Infinity by Zero or divide Infinity by Infinity will result in NaN. Subtracting Infinity by Infinity also results in NaN.

                            
console.log(Infinity*0)
console.log(Infinity/Infinity)
console.log(Infinity-Infinity); 
        
                        
                            
                        

Operations involving NaN

All operations involving NaN results in a NaN.

                            
console.log(Number.NaN+1)
console.log(Number.NaN*1)
console.log(Number.NaN/1)
console.log(Number.NaN-1)
        
                        
                            
                        

Comparing NaN

Any comparison between the two NaN's always results in false. NaN is never equal to itself.

                            
let n1=Number.NaN;       //create a NaN
let n2=n1;               //make it equal
 
console.log(n1 == n2)     //false.  
console.log(n1 != n2)     //true
        
                        
                            
                        

In the following example, n1 & n2 are NaN's. But they are neither equal or greater or lesser from each other.

                            
let n1=Number.NaN;
let n2=Number.NaN;
 
console.log(n1 == n2)      //false
console.log(n1 > n2)      //false
console.log(n2 < n1)      //false 
        
                        
                            
                        

How to check if the value is NaN

There are two ways in which we can check if the value is NaN. One is using the isNaN function and the other is to use the Number.isNaN method.

IsNaN

We can check whether a value is NaN by using the global isNaN method.

                            
 console.log(isNaN(NaN));    //true 
        
                        
                            
                        

The following code works because isNaN tries to convert the “test” into a number, which results in a NaN value. Hence the output is true.

                            
 console.log(isNaN("Test"));    //true
        
                        
                            
                        

Number.IsNaN

You can also use the Number.isNaN method, But it does not forcefully convert the parameter to a number. If the argument is not a number, then it results false.

                            
console.log(Number.isNaN(NaN));      //true
console.log(Number.isNaN("Test"));   //false
        
                        
                            
                        

Booleans are not NaN

Booleans are implemented as numerical values with a single binary digit (i.e., 0 & 1). Hence they are treated as numbers.

                            
isNaN(true);  // false
isNaN(false); // false
        
                        
                            
                        

Summary

When a proper number cannot be the result of numerical operations, then the result is NaN. For Example, converting a string to a number. NaN is a primitive number. The NaN can appear when you convert an Invalid value to a number, Divide Zero by Zero, Some operations involving infinity & all Operations involving NaN, etc. You can check if the value is NaN by using the method IsNaN or Number.IsNaN

Read More

  1. JavaScript Tutorial
  2. Data Types in JavaScript
  3. JavaScript String
  4. Template Strings & String Interpolation
  5. Tagged Templates
  6. String to Number
  7. EPSILON & Floating Point Precision
  8. Infinity
  9. BigInt
  10. BintInt Vs Number
  11. Boolean Data Type
  12. Undefined