Confusing Javascript Terms: isNaN and NOT(!) Operator

Abdullah Jimoh
2 min readNov 13, 2021
Javascript concept
Javascript concept

Today, we will have an in-depth understanding of isNaN javascript inbuilt function and ! (Not) logical operator and how to use these two terms effectively in our programs.

isNaN and Not(!) operator are one of many confusing terms in Javascript for beginners. isNaN being “is Not a Number” written as isNaN() is an inbuilt javascript function that takes a value isNaN(value). isNaN() returns a boolean true or false that indicates if the value is a Number or Not a Number.

Example

isNaN(5)    //false
isNaN("Yes") //true

The isNaN() returns true if the given value is Not a Number. Why?

The isNaN has a special case behavior in its earliest version: if the given argument is not a type of number (i.e 1, 2, 3) the function will perform type conversion on the value which will coerce it to a number and determine the resulting value.

CONFUSED??

Are you confused?

Okay, let’s try to understand NaN (Not a Number). NaN is not the opposite of a number. Take, for example, a "string" which is "not a number" but it does not equate to NaN. So, a string is "Not a Number". If you think of it in its literal term, you might think whatever value is Not a Number must be a Number. This wrong!

NaN means an Invalid Number. You get this when you are trying to operate on a value that isn't a number.

This is why Number.isNaN() method was introduced in ES6. It solves the false positive problem of isNaN .

Example:

Number.isNaN(5)    //falseNumber.isNaN("Yes")    //fals

What happened here??

Number.isNaN() doesn't do any type of coercion, unlike the previous method isNaN. in other words, it doesn't convert its argument. So, it is always advisable to use Number.isNaN() when checking if a value is NaN.

NOT(!) Operator

! (Not) is a logical operator that returns the inverse value of a boolean type. That is, it converts truth to false and false to truth.

Example:

let accepted = true
let rejected = false

console.log(!accepted ) //false
console.log(!rejected ) //true

what happened here is, the ! operator converts the accepted to false and the rejected to true.

Next time, we will be looking into other confusing concepts in Javascript.

--

--

Abdullah Jimoh

Software Engineer | DevOps Engineer | Technical Writer