What is an NaN error in JavaScript?

What is an NaN error in JavaScript?


2 min read


Have you ever wondered what is NaN error coming in your project? ๐Ÿค”

Today, in this article I am going to discuss the NaN error in detail.

Let's get started ๐Ÿš€

In JavaScript, NaN stands for Not a Number. This error occurs when you parse something to a number that is not a number

Let's see it with an example,

var helloWorld = parseInt(helloWorld);

This will return NaN

didn't get it? No problem let's get a little simpler

var helloWorld = parseInt("Hello World");

"Hello World" is a string and we are parsing to an integer but that is not possible therefore the browser will return NaN

isNan() Method

isNan() will return true if a value is NaN example

// false
// false
// true
// false
// true

As you can see numbers will return false as they are not NaN even if, the number is in the form of string. Any string (word or sentence) will return true as it is NaN

Me when isNaN("123") showing false: confusing

What about you? Comment ๐Ÿ‘‡

wait a minute

Here comes, something that contradicts

As discussed earlier, isNan() will return true if a value is Not-a-Number(NaN) Number.isNaN() method while is completely opposite of isNaN method, here Number.isNaN() will return true if number is NaN

Let's go with an example,

// true
// false
// false
// true

Thank you for reading, have a nice day!