Sounds a bit difficult? Not an issue. Let’s simplify this by our favorite section of the blog which is nothing but code section.
/* Calling global isNaN() function by window object */
window.isNaN(); // true
/* Calling isNaN() function of Number constructor*/
Number.isNaN(); // false
Outputs are little interesting. isn’t it? 🙂 🙂
I know, your mind is jumping with question i.e. “how they are giving different outputs?” That is the exact thing which we will discuss here. Let’s start.
1. window().isNaN(): An argument passed in window.isNaN(), first converts it to the number then performs checking on it. Let’s try to understand above example.
In above case, we are passing blank as a parameter it means passed argument not defined. It will be converted as window.isNaN(Number(undefined)). If we check Number(undefined) on the console then it’s output is NaN. Now, function became as window.isNaN(NaN) which will be definitely true. Got it.
2. Number.isNaN(): The argument passed in Number.isNaN() only performs checking on it, not any type conversion. Let’s try to understand above example again.
Number.isNaN() will translated into Number.isNaN(undefined) as passed argument is not defined. And obviously, it will be returned as false because undefined in not NaN. You can check undefined == NaN on your console which will return false.
Let’s take some other examples:
/* Perfoming on Number constructor
Number.isNaN('') // false
Number.isNaN(true) // false
Number.isNaN(undefined) // false
Number.isNaN('NaN') // false
Number.isNaN(NaN) // true
Number.isNaN(0 / 0) // true
/* Perfoming on window object
window.isNaN('') // false (Number('') will become 0)
window.isNaN(true) // false (Number(true) will become 1)
window.isNaN(undefined) // true
window.isNaN('NaN') // true
window.isNaN(NaN) // true
window.isNaN(0 / 0) // true
Hey! congratulation dude, you have done with it. Now you can perform checking on the numbers by your own preferences. This blog give some more arrows in your bow.
Thanking you all,
Any type of feedback or comment is really appreciated. I am eagerly waiting for those.
Hope to see you soon, until then Happy Learning …… 🙂 🙂 🙂