YogeshChauhan . com

The Difference Between isNaN() Method And isNaN() Function In JavaScript

in JavaScript on September 5, 2020

JavaScript isNaN() Function

The isNaN() function determines whether a value is an illegal number (Not-a-Number).

This function returns true if the value equates to NaN. Otherwise it returns false.

NaN values are generated when arithmetic operations result in undefined or unrepresentable values.

A NaN also results from attempted coercion to numeric values of non-numeric values for which no primitive numeric value is available.



where value = The value to be tested

Why do we need this function?

Unlike all other possible values in JavaScript, it is not possible to rely on the equality operators (== and ===) to determine whether a value is NaN or not.

As we saw in this post already both NaN == NaN and NaN === NaN evaluate to false. Hence, the necessity of an isNaN function.


console.log(isNaN(-1.99)); //false
console.log(isNaN(4-2)); //false
console.log(isNaN(0)); //false
console.log(isNaN('1')); //false
console.log(isNaN('string')); //true
console.log(isNaN('2020/5/5')); //true
console.log(isNaN('')); //false
console.log(isNaN(true)); //false
console.log(isNaN(undefined)); //true
console.log(isNaN('NaN')); //true
console.log(isNaN(NaN)); //true
console.log(isNaN(0 / 0)); //true

JavaScript Number isNaN() Method

The Number.isNaN() method determines whether a value is NaN (Not-A-Number).

This method returns true if the value is of the type Number, and equates to NaN. Otherwise it returns false.


Number.isNaN(1) //false
Number.isNaN(-1.99) //false
Number.isNaN(4-2) //false
Number.isNaN(0) //false
Number.isNaN('1') //false
Number.isNaN('string') //false
Number.isNaN('2020/5/5') //false
Number.isNaN('') //false
Number.isNaN(true) //false
Number.isNaN(undefined) //false
Number.isNaN('NaN') //false
Number.isNaN(NaN) //true
Number.isNaN(0 / 0) //true

You must have noticed the difference but if not, let me point it out.

//function results

console.log(isNaN('string')); //true
console.log(isNaN('2020/5/5')); //true

//method results
Number.isNaN('string') //false
Number.isNaN('2020/5/5') //false

This function is different from the Number specific Number.isNaN() method.

The global isNaN() function, converts the tested value to a Number, then tests it.

Number.isNaN() does NOT convert the values to a Number, and will not return true for any value that is not of the type Number.

NOTE: Examples are from w3schools


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Jun 16 What are Stored Procedures for SQL Server? Jun 16 What are Class Constants in PHP? Jun 15 A short basic guide on states in React Jun 15 How to define constants in PHP? Jun 15 How to define visibility for a property in PHP? Jun 15 How to use @if and @else in SCSS?

You might also like these

Overview of Drupal and how it worksDrupalIntroduction to components and templates Part 3: Data bindingAngularHow to get Current Year, Month and Date in JavaScript?JavaScriptWhat is the best way to add JavaScript Code into HTML?HTMLCasting in PostgreSQLPostgresKeyValuePipe in Angular 9Angular