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 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 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
How to create a simple slider with CSS and jQuery?CSSJavaScript String Properties and MethodsJavaScriptWhat happened to your Google Analytics Tracking ID, can’t find it?MiscellaneousHow to force your website to load securely with an .htaccess file?MiscellaneousHow to deploy Angular App on Dreamhost or Amazon S3 or firebase?AngularHow to render Lists in React?React