Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Deciphering isNaN()
2.1.
Syntax of isNaN()
3.
Using isNaN() in JavaScript
4.
Pitfalls with isNaN()
5.
Frequently Asked Questions
5.1.
What does isNaN() do in JavaScript?
5.2.
What type of values does isNaN() return?
5.3.
What's the difference between isNaN() and Number.isNaN()?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

isNaN() in JavaScript

Author Sinki Kumari
0 upvote
Create a resume that lands you SDE interviews at MAANG
Speaker
Anubhav Sinha
SDE-2 @
12 Jun, 2024 @ 01:30 PM

Introduction

Hey there, 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. 

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.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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 isNaN() do in JavaScript?

In JavaScript, the isNaN() function determines if a value is an illegal number (NaN).

What type of values does isNaN() return?

isNaN() returns a Boolean value. It returns true if the given value is NaN and false if it's not.

What's the difference between isNaN() and Number.isNaN()?

Number.isNaN() is stricter than isNaN() as it only returns true if the argument is strictly 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:

If you liked our article, do upvote our article and help other ninjas grow.  You can refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more!

Happy Coding!
 

Previous article
Javascript Form Validation
Next article
Promise .then() Function in JavaScript
Live masterclass