Learn NaN, isNaN like a chad

Learn NaN, isNaN like a chad

Last Article about NaN that you'll ever read

Introduction

Hey Guys, if you are familiar with JavaScript, either a beginner or someone who is working for quite a few time, have seen the keyword NaN. It is a special reserved keyword in JavaScript which means 'Not A Number'. Further Reading you'll get to learn more about it and will learn various methods associated with it. So, Let's jump in.

js.jpeg

If you are using JS for quite some time, then you must've heard of the method 'typeOf' which lets us determine the datatype of a variable which we pass as an argument. Now, if we do typeOf(NaN) then you'll be surprised to know its output 'number'.

console.log(typeOf(NaN)) //'number'

What is NaN?

Now you guys have been thinking what exactly is this NaN? Its type is 'number' but where do we use it? So, let me tell you, NaN is used when to suppose your function was meant to return a number value but instead you got some different calculations and you don't want to return 0 or -1 so you can return 'Nan' in that case, it'll simply mean that your function is not behaving the same as you expected it to be.

Where you'll find it?

You can also encounter 'NaN' while using some operators in Javascript. Analyse this following code and you'll get to know while performing these operations, JS will type coerce all the strings which will make it an invalid operator ( int - string ), so you'll get NaN in the output.

let a = 'abc' - 'bc';
console.log(a); //NaN

let b = 10 - 'a';
console.log(b); //NaN

let c = 'a' - 10;
console.log(c); //NaN

let d = 10*'a';
console.log(d); //NaN

let e = 10/'a';
console.log(e); //NaN

let f = Math.sqrt("abcde");
console.log(f); //NaN

Equality Check

Now, if you're amazed enough by the keyword NaN, let me show you some more astonishing properties of it.

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

When you'll do equality check on NaN, you'll realise it is not even to equal to itself which is true eventually. Because NaN doesn't has any particular value. It's not like we can say NaN is equal to -1 or infinity or something else. NaN simple donates that your particular variable is not a valid number and doesn't state anything about the value of that variable. Now if you want to know the exact regarding this behaviour of NaN while equality check you can read the official documentation of Abstract Equality Comparison and Strict Equality Comparison. You'll understand why you get false in output when any of the value in equality check is NaN.

How to check value is NaN?

Now to check whether a certain value is NaN or not, there are two most widely most used in-built functions isNaN() and Number.isNaN(). Keep reading and you'll realize which method is correct and which method you should use in your code.

isNan()

Now, if you try isNaN in your code, you'll get these following outputs.

let g = 8;
console.log(isNaN(g)); //false

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

isNaN() function will tell you whether the value which you've passed as argument is NaN or not. But there is a flaw in isNaN() function, what if you pass a string as an argument in isNaN() function what will happen then? Let's try it out.

let i = "abc";
console.log(isNaN(i)); //true

Wait... What? Yes this is the flaw in isNaN() function, it doesn't tell you about whether the argument is NaN or not it'll tell you about the argument which you've passed is Not A Number or not. So, in that case our method is correct, because strings are not a number that is why isNaN() is giving true as output when a string is passed in it.

But, our main agenda was to check whether the argument is NaN or not?

So, we can do a little change in our code and it'll tell you whether your argument is NaN or not?

let j = 8;
console.log((typeof(j)=='number' && isNaN(j))?"Nan":"Not Nan"); // Not NaN

let k = NaN;
console.log((typeof(k)=='number' && isNaN(k))?"Nan":"Not Nan"); // NaN

isNaN() will give true output if we pass NaN or a string to it, so if we add a check that only those variables are considered whose type is number then our method will work fine and it'll give us the desired result because all the string values are eliminated when we check its type using typeOf.

Number.isNan()

As you guys have seen above our agenda is partially fulfilled when we used isNaN() method we'd to write some additional piece of code to make it work because isNaN() works only with type number. So instead of isNaN() we can use Number.isNan() method, which behind the scenes work exactly the same as we've written in the code above.

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

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

let n = "cool";
console.log(Number.isNaN(n)); //false

As you can see it is working perfectly fine for both string values and NaN. So, Number.isNaN() method is used more than isNaN() you know the reason why is it so!?

Learnings

Let's summarize what we've learnt in this blog?

  • NaN special keyword in JS denotes 'Not a Number'
  • NaN != NaN
  • isNaN() works only when type is number
  • Number.isNaN() works well for all case.

Thank you guys for reading my blog and yeah pardon me for any mistakes. PEACE🤞