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 😊.

Frequently Asked Questions

Is JavaScript still worth learning in 2026?
Yes. JavaScript runs on 98% of websites for the front-end, dominates the back-end via Node.js, powers mobile apps through React Native, builds desktop tools through Electron, and is the scripting layer for most AI tooling (LangChain.js, OpenAI SDK, Vercel AI). Whether you target web, mobile, AI, or full-stack capstones, JavaScript is the broadest single language you can learn.
What is the difference between var, let, and const?
var is function-scoped, hoisted to the top of its scope, and can be redeclared, which leads to bugs in modern code. let is block-scoped (only visible inside the nearest {}) and can be reassigned. const is block-scoped and cannot be reassigned, although object contents can still mutate. Default to const for everything, switch to let only when you actually need to reassign, and avoid var in any code written after 2017.
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds useful features like top-level await, private class fields with the # prefix, and the .at() array method. If you are writing for older browsers (IE11 or older Android WebViews), transpile down with Babel or use a build tool like Vite, esbuild, or webpack.
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard, free, with built-in IntelliSense, debugger, terminal, Git, and a huge extension marketplace (ESLint, Prettier, GitHub Copilot, Tailwind). Install the JavaScript and TypeScript Nightly extension for the latest language features. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, the Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools with F12 (or right-click then Inspect), go to the Console tab, type or paste your code, press Enter. For HTML pages, add a script tag pointing to your .js file. Locally with Node.js: download Node from nodejs.org (LTS version), then run node script.js in your terminal from the file folder. Use the same Node setup for backend capstones, API integrations, and scripts that do not need a browser.
What can I build with JavaScript for my BSIT capstone?
Common BSIT capstones in JavaScript: full-stack web apps using React or Vue on the front-end with Node.js and Express on the back-end (MongoDB or MySQL for the database), real-time chat or notification systems using Socket.io, single-page dashboards with Chart.js or D3.js, cross-platform mobile apps with React Native, AI-powered chatbots using OpenAI SDK and LangChain.js, and Chrome extensions for productivity tools. Add Tailwind CSS for the UI and Vercel or Netlify for free deployment.

Leave a Comment