How to check if a number is NaN or Infinity in JavaScript?

Explore how to check for NaN and Infinity values in JavaScript with our easy-to-understand guide.

Don’t miss out on this valuable information, so keep reading to learn more!

In this article, you’ll also understand the differences between the isNaN()Number.isNaN()isFinite(), and Number.isFinite() methods and know when to use each one.

What is NaN in JavaScript with example

NaN is an abbreviation for “Not a Number” and it’s a term used in JavaScript to describe a value that is not a valid number.

It comes up when a math operation doesn’t give a valid or meaningful result. If you try dividing zero by zero or doing calculations with non-numeric values, you’ll end up with NaN.

In JavaScript, NaN is a numeric data type, but it’s not equal to anything, not even itself. So, if you compare a value directly with NaN, it will always be false.

Here’s an example:

let a = 0/0; // Division by zero
console.log(a); // NaN

let b = Math.sqrt(-1); // Square root of a negative number
console.log(b); // NaN

let c = "Itsourcecode" * 2; // Multiplying a string by a number
console.log(c); // NaN

Output:

NaN
NaN
NaN

Take note, NaN is not the same as any other value, including itself. As a result, you can’t use the == or === operators to check for NaN.

Instead, you can use the isNaN() function or the Number.isNaN() method to see if a value is NaN.

How to check if a number is nan or infinity in JavaScript?

How to check if value is NaN in JavaScript, you can use the following solutions:

Solution 1: Use isNaN() function

The isNaN() function in JavaScript is used to figure out if a value is NaN or can be turned into a number.

It takes one input and tells you “true” if the input is NaN or can’t be converted into a proper number. If the input can be converted into a valid number, it returns “false.”

Syntax:

isNaN(parameter)

Here’s an example code:

let a = NaN;
console.log(isNaN(a)); // true

let b = "Itsourcecode";
console.log(isNaN(b)); // true

The isNaN() function tells you if a value is NaN or not. However, it’s important to know that it tries to convert the value into a number first. This can sometimes cause unexpected results.

For example, if you use isNaN(“Itsourcecode”), it will say it’s NaN even though “Itsourcecode” is not a number.

Output:

true
true

Solution 2: Use Number.isNaN() method

If you want to check whether a value is NaN or not, the Number.isNaN() method is your best bet. It’s a more reliable way to do the test.

This method will only tell you it’s true if the value is currently NaN.

Syntax:

Number.isNaN(parameter)

Here’s an example code:

let a = NaN;
console.log(Number.isNaN(a)); // true

let b = "Itsourcecode";
console.log(Number.isNaN(b)); // false

The Number.isNaN() method specifically looks for numeric types of variables. This means that if we provide variables of any other data type besides a number, it will always give us a false result.

Output:

true
false

Solution 3: Use isFinite() function

The isFinite() function works like this: if the value is positive or negative Infinity, it says “false.” But if it’s any other number, it says “true.”

Here’s an example code:

let a = Infinity;
console.log(isFinite(a)); // false

let b = -Infinity;
console.log(isFinite(b)); // false

let c = 1/0;
console.log(isFinite(c)); // false

let d = Math.log(0);
console.log(isFinite(d)); // false

Output:

false
false
false
false

Solution 4: Use Number.isFinite() method

The Number.isFinite() method works just like the isFinite() function, but it doesn’t try to change or convert the data type of the value.

Here’s an example code:

let a = Infinity;
console.log(Number.isFinite(a)); // false

let b = -Infinity;
console.log(Number.isFinite(b)); // false

let c = 1/0;
console.log(Number.isFinite(c)); // false

let d = Math.log(0);
console.log(Number.isFinite(d)); // false

Output:

false
false
false
false

Here is a complete code example that illustrates how to determine whether a number is NaN or Infinity in JavaScript, using the methods I mentioned earlier.

let values = [NaN, "Itsourcecode", 18, Infinity, -Infinity, 0, 2e64];

console.log('Using isNaN() function:');
values.forEach(value => {
    console.log(`isNaN(${value}) = ${isNaN(value)}`);
});

console.log('\nUsing Number.isNaN() method:');
values.forEach(value => {
    console.log(`Number.isNaN(${value}) = ${Number.isNaN(value)}`);
});

console.log('\nUsing isFinite() function:');
values.forEach(value => {
    console.log(`isFinite(${value}) = ${isFinite(value)}`);
});

console.log('\nUsing Number.isFinite() method:');
values.forEach(value => {
    console.log(`Number.isFinite(${value}) = ${Number.isFinite(value)}`);
});

Output:

Using isNaN() function:

isNaN(NaN) = true
isNaN(Itsourcecode) = true
isNaN(18) = false
isNaN(Infinity) = false
isNaN(-Infinity) = false
isNaN(0) = false
isNaN(2e+64) = false

✅Using Number.isNaN() method:

Number.isNaN(NaN) = true
Number.isNaN(Itsourcecode) = false
Number.isNaN(18) = false
Number.isNaN(Infinity) = false
Number.isNaN(-Infinity) = false
Number.isNaN(0) = false
Number.isNaN(2e+64) = false

✅Using isFinite() function:

isFinite(NaN) = false
isFinite(Itsourcecode) = false
isFinite(18) = true
isFinite(Infinity) = false
isFinite(-Infinity) = false
isFinite(0) = true
isFinite(2e+64) = true

✅Using Number.isFinite() method:

Number.isFinite(NaN) = false
Number.isFinite(Itsourcecode) = false
Number.isFinite(18) = true
Number.isFinite(Infinity) = false
Number.isFinite(-Infinity) = false
Number.isFinite(0) = true
Number.isFinite(2e+64) = true

How to check if a variable is NaN in JavaScript?

You can use the isNaN() function to check if a variable is NaN (Not-a-Number) in JavaScript. This function returns true if the value is NaN and false otherwise.

Here’s an example:

let a = NaN;
console.log(isNaN(a)); // true

let b = 10;
console.log(isNaN(b)); // false

However, it’s important to remember that the isNaN() function has its limitations. It may mistakenly return true for values that are not NaN, but are not of the number type.

Let me show you an example:

let b = "Itsourcecode";
console.log(isNaN(b)); // true

Conclusion

In conclusion, this article provides different solutions on how to check if a number is NaN or Infinity in JavaScript.

We cover the concept of NaN and provide clear explanations and practical examples.

This article presents four solutions: using isNaN(), Number.isNaN(), isFinite(), and Number.isFinite(). It concludes with a code example showcasing these methods.

We are hoping that this article provides you with enough information that helps you understand the JavaScript check if nan.

You can also check out the following article:

Thank you for reading itsourcecoders 😊.

Leave a Comment