Table of contents
1.
Introduction
2.
What Is isNaN() in Javascript?
3.
Deciphering isNaN()
3.1.
Syntax of isNaN()
4.
Using isNaN() in JavaScript
5.
Pitfalls with isNaN()
6.
Frequently Asked Questions
6.1.
What does the isNaN() function in JavaScript do?
6.2.
Why NaN == NaN is false?
6.3.
What is the difference between NaN and isNaN?
7.
Conclusion
Last Updated: Jan 21, 2025
Easy

isNaN() in JavaScript

Author Sinki Kumari
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

JavaScript enthusiasts! Today, we're going to delve into a valuable tool in your JavaScript toolkit - the isNaN() function. This simple yet powerful function can help you avoid common pitfalls in dealing with data in JavaScript.

isNan() in javascript

In this blog, we will learn about isNan() function in JavaScript. 

What Is isNaN() in Javascript?

isNaN() is a JavaScript function that checks whether a value is NaN (Not-A-Number). It returns true if the value is NaN and false otherwise. isNaN() attempts to convert the argument to a number before checking. If the argument cannot be converted to a valid number, it returns true; otherwise, it returns false.

Deciphering isNaN()

In Javascript, isNaN() is a global function used to determine if a value is an illegal number (Not-a-Number or NaN). It's a widely-used method because of JavaScript's dynamic and loosely-typed nature. isNaN() takes one argument and returns a Boolean value depending on whether the provided value equates to NaN.

Syntax of isNaN()

The syntax of isNaN() function is pretty straightforward:

isNaN(testValue)

Here, testValue is the value you want to check. If testValue can be converted into a legal number, isNaN() returns false. If not, it returns true.

Using isNaN() in JavaScript

Let's take a look at some examples to understand how isNaN() works:

console.log(isNaN("Hello")); 

Output

true
output
console.log(isNaN("2005/12/12")); 

Output

true
output
console.log(isNaN(123)); 

Output

false
output
console.log(isNaN('123')); 

Output

false

In the above example, "Hello" and "2005/12/12" can't be converted to a legal number, so isNaN() returns true. On the other hand, 123 and "123" can be converted to a legal number, so isNaN() returns false.

Pitfalls with isNaN()

Although isNaN() is very useful, it has a small pitfall. When the argument is an object, JavaScript first attempts to convert this object into a primitive value and then checks if that's NaN. This can lead to unexpected results:

console.log(isNaN({})); // true
console.log(isNaN(["apple"])); // true

In these cases, it's often safer to use Number.isNaN() function, which was added in ECMAScript 6 (ES6) and only returns true if the argument is strictly NaN.

Frequently Asked Questions

What does the isNaN() function in JavaScript do?

isNaN() checks whether a value is NaN (Not-A-Number). It returns true if the value cannot be converted to a valid number.

Why NaN == NaN is false?

NaN is not equal to any value, including itself, due to the IEEE floating-point standard. Therefore, NaN == NaN evaluates to false.

What is the difference between NaN and isNaN?

NaN is a special value representing an invalid number, while isNaN() is a function that checks if a value is NaN.

Conclusion

To sum it up, the isNaN() function is a fundamental tool in JavaScript for handling and validating data. Although it has its nuances, with careful use and understanding, it can be an asset in ensuring your JavaScript code handles numbers properly. It's another reminder of how JavaScript's dynamic nature can be both a boon and a challenge. Keep learning and happy coding!

To learn more about JavaScript, we recommend reading the following articles:

Live masterclass