Pair Of Concepts May Confuse You in Javascript — Number.isNaN() and IsNaN()

Pair Of Concepts May Confuse You in Javascript — Number.isNaN() and IsNaN()

Pair of concepts may confuse you in Javascript — Number.isNaN() and isNaN() - As you can see NaN even can not be compared to itself, so how we can detect if a variable is NaN, before ES6 we can use the function isNaN(). The isNaN check in JS always had its issue. Term, you might interpret it as whatever is "not a number"

With a long history of development (and chaos), JS has some messy legacy that can’t be removed to keep the consistency but only improved by new features/functions. This leads to confusion for developers. The series has been written as  notes for myself and others to comprehend these concepts and avoid bugs in development.

What is NaN ?

NaNis shorthand for Not A Number, specified in  The IEEE Standard for Floating-Point Arithmetic (IEEE 754–2008) for floating-point arithmetic established in 1985. In Javascript context, it is a “_property of the global object. In other words, it is a variable in global scope._”. It has the below characteristics:

  • It is considered a Number type

  • Equalivent to Number.NaN

NaNis the only value in JavaScript which is not equal to itself.

  • It’s falsy

  • Equalivent to Number.NaN

NaNis the only value in JavaScript which is not equal to itself.

  • It’s falsy

Take a look at some examples, and think about the results !?

console.log(NaN === NaN) // false

console.log(NaN == NaN) // false
console.log(NaN !== NaN) // true
console.log(NaN != NaN) // true
console.log(typeof(NaN)) // number
a = NaN;

Global isNaN()

As you can see NaN even can not be compared to itself, so how we can detect if a variable is NaN, before ES6 we can use the function isNaN() yet considered the following examples.

isNaN(NaN); // true

isNaN('NaN');   // true
isNaN(undefined); // true
isNaN({}); // true
isNaN('Techika.com'); // true
isNaN(''); // false
isNaN('12abcd') // true

javascript

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Hire Dedicated JavaScript Developers -Hire JavaScript Developers

Hire dedicated JavaScript Developers who are proficient in AngularJS, ReactJS, NodeJS, & VueJS frameworks. Get flexible hiring models as per your business requirements.

What is JavaScript - Stackfindover - Blog

What is PHP: - Who invented PHP, how it works, answers to all such questions about PHP, and much other information, you are going to

JavaScript Shopping Cart - Javascript Project for Beginners

JavaScript Shopping Cart - javascript shopping cart tutorial for beginnersBuy me a coffee 🍺 https://www.paypal.com/paypalme/ziddahSource Code: https://bit....

The essential JavaScript concepts that you should understand

The essential JavaScript concepts that you should understand - For successful developing and to pass a work interview

JavaScript compound assignment operators

We use assignment operator in JavaScript to store values in a variable. Read more...