How To Check If A Number Is NaN In JavaScript

Check if a Number is NaN in JavaScript

In this article, you’ll learn how to check if a number is NaN in JavaScript by checking for falsy values or using the .isNaN() and Number.isNaN() functions. Let’s read this article now.

Understanding the NaN data type

NaN or literally “Not a Number” is a data type that represents anything which can not be mathematically represented or a number that’s considered undefined.

It would include things like infinity or a number divided by zero or the square root of a negative number etc.

One thing to note is that NaN is not the same as undefined, though it acts similarly. If you want concrete proof of this, you can use the typeof keyword, in which NaN will return number and undefined will return undefined.

More importantly, it helps you check for NaN values if it happens because you can look for anywhere on your code that could be mathematically unrepresentable.

Check if a number is NaN in JavaScript

First thing to keep in mind is that you can’t test for NaN because NaN is more of a concept than an actual value. As such, you cannot check for NaN using equality (==, !=) or strict equality (===, !==), not even with itself. See here:

Code:

console.log(2 / 0 == NaN);
console.log(2 / 0 === NaN);
console.log(NaN == NaN);
console.log(NaN === NaN);
console.log(2 / 0 != NaN);
console.log(2 / 0 !== NaN);
console.log(NaN != NaN);
console.log(NaN !== NaN);

Output:

false
false
false
false
true
true
true
true

As a side note, technically speaking, NaN is the only value that will return true on x !== x, so you could technically check if a value is NaN or not by doing that. However, it does not take in consideration for strings, which returns false like every other value that isn’t NaN in this situation.

Checking for falsy values

For anyone who doesn’t know, a falsy value is any value that is technically considered false if used in the same context as a Boolean. Some of these values are: undefined, 0, null, empty strings ("", '', ``) and important to this topic – NaN.

So using this fact, we can check whether a number is NaN by using the value in a Boolean context. As such:

Code:

const n1 = 6;
const n2 = NaN;
const n3 = 0;
const n4 = "3";
const n5 = "LearnShareIT";

function check_isNaN(n) {
	if (Number(n)) {
		console.log("Is a Number");
	} else {
		console.log("Not a Number");
	}
}

check_isNaN(n1);
check_isNaN(n2);
check_isNaN(n3);
check_isNaN(n4);
check_isNaN(n5);

Output:

Is a Number
Not a Number
Is a Number
Is a Number
Not a Number

Notice how we don’t check for true/false use == or !=. As explained above, this will not work. Additionally, you can see the issue here as 0 is considered “Not a number”. That is because 0 is also a falsy value, so you would need to check for that as well. Here’s the revised code:

Code:

const n1 = 6;
const n2 = NaN;
const n3 = 0;
const n4 = "3";
const n5 = "LearnShareIT";

function check_isNaN(n) {
	var comment = "Is a Number";
	if (!Number(n)) {
		comment = "Not a Number"
	}
	if (n === 0) {
		comment = "Is a Number";
	}

	console.log(comment);
}

check_isNaN(n1);
check_isNaN(n2);
check_isNaN(n3);
check_isNaN(n4);
check_isNaN(n5);

Output:

Is a Number
Not a Number
Is a Number
Is a Number
Not a Number

While this fixes the issue of falsy values, we now have to deal with the fact that something like a string does fall under the conditions of “Not falsy” and “Not 0”. To deal with that, we’re better off using built-in functions like .isNaN() and its a better version Number.isNaN().

Using Number.isNaN()

Note while there is technically a built-in isNaN() function, it’s generally not used due to its inconsistency. So practically you should always stick to the Number class’ .isNaN() function as it’ll work exactly as intended. For a more descriptive explanation of why the .isNaN() function is not very good, read here.

Though one thing to note is that it is necessary to have the value inside the Number class’ constructor – Number(val) to avoid converting the string into a number.

Here’s the same example:

Syntax:

Number.isNaN(value)

Returns: Boolean

Parameters:

NameTypeDescription
valueValue to check if is NaN will be converted into a number (though not the number class)

Code:

const n1 = 6;
const n2 = NaN;
const n3 = 0;
const n4 = "3";
const n5 = "LearnShareIT";

function check_isNaN(n) {
	const N = Number(n);
	if (Number.isNaN(N)) {
		console.log("Not a Number");
	} else {
		console.log("Is a Number");
	}
}

check_isNaN(n1);
check_isNaN(n2);
check_isNaN(n3);
check_isNaN(n4);
check_isNaN(n5);

Output:

Is a Number
Not a Number
Is a Number
Is a Number
Not a Number

Summary

To check if a number is NaN in JavaScript, you could check for falsy values, use the .isNaN() or Number.isNaN() function (though preferably the latter).

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *