Since ES6’s release, many new, comfy, and handy native methods have been added into the new standard of JavaScript.
Yet, I’ve seen many old codes from GitHub repositories. It doesn’t necessarily mean they’re bad — but these features I’m going to introduce to you will help your code be more readable and prettier.
NaN
is a number type.
typeof NaN === 'number'
So you can’t distinguish NaN
and numbers.
Even Object.prototype.toString.call
returns [object Number]
for both NaN
and numbers. You might have known there’s the isNaN
method to check if the parameter is NaN
. But since ES6, the number
constructor has started to include isNaN
as its method. Then what’s different?
isNaN
— checks whether the passed value isn’t a number or can’t be converted into a number.Number.isNaN
— checks whether the passed value isn’t a number.Here’s the example. And this topic was already dealt with by people at Stack Overflow.
Number.isNaN({});
// <- false, {} is not NaN
Number.isNaN('ponyfoo')
// <- false, 'ponyfoo' is not NaN
Number.isNaN(NaN)
// <- true, NaN is NaN
Number.isNaN('pony'/'foo')
// <- true, 'pony'/'foo' is NaN, NaN is NaN
isNaN({});
// <- true, {} is not a number
isNaN('ponyfoo')
// <- true, 'ponyfoo' is not a number
isNaN(NaN)
// <- true, NaN is not a number
isNaN('pony'/'foo')
// <- true, 'pony'/'foo' is NaN, NaN is not a number
#javascript #react #nodejs #angular #es6