Yogesh Chauhan's Blog

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.

Syntax


isNaN(value)

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.

Examples


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.

Examples


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 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
How to define variables in SCSS (Sass)?SCSSSubquery in PostgresPostgresHow to convert an HTML radio buttons into a toggle switch using CSS?CSSMIN, MAX, COUNT, AVG and SUM in SQLSQL/MySQL2 Ways We Can Write Multiple Line Commands in PHPPHPArray destructuring and Object destructuring in JavaScriptJavaScript