DEV Community

Discussion on: 20 Killer JavaScript One-Liners That’ll Save You Hours of Coding 🤯🔥

Collapse
 
pcjmfranken profile image
Peter Franken • Edited

Careful, JS is effing weird:

!isNaN(parseFloat('Infinity')) // true
typeof NaN === 'number' // true (lol)
typeof null === 'object' // true
typeof Math === 'object' // true
typeof MyClass === 'function' // true
typeof new Date() === 'object' // true
Enter fullscreen mode Exit fullscreen mode

There's more where that came from, but the gist of it is that whenever JS claims something is an object, function, or numerical value it's probably lying or not telling the whole story. Mistakes with JS types can lead to some very nasty bugs, so always make sure you know what is actually going on with your code!

I highly recommend reading up on how prototypes work, or at the very least check out this this MDN page on typeof's various gotcha's: developer.mozilla.org/en-US/docs/W...

Collapse
 
rammcodes profile image
Ram Maheshwari ♾️

Thanks for sharing 🙌

Collapse
 
codingjlu profile image
codingjlu

IMO infinity is not a number.

Collapse
 
scriptneutron profile image
script-neutron

Yes

Collapse
 
josefheld profile image
Josef Held

Well, in JS it is a number.
maybe calling it Infinity was a poor choice 😉

Collapse
 
pcjmfranken profile image
Peter Franken

Agreed, which makes it all the more useful to know that it's classified as one in JS anyway (and in fact by most other software and runtimes too, due to the IEEE 754 standard for floating-point arithmetic).

The isFinite global function (spec, MDN) might come in handy next time you need to validate a numerical type.

Collapse
 
kitanga_nday profile image
Kitanga Nday

Infinity is a number in math. I think it's set this way for internal reasons, but outside of programming infinity is a number

Thread Thread
 
pcjmfranken profile image
Peter Franken

At the risk of sounding super pedantic, it would be more accurate to say infinity represents a number (a quantity) rather than actually being one itself. Unlike maths' infinity which can actually represent different measures, JS' version is essentially just an "out of bounds" indicator (x+n) within our "fake" digital/binary arithmetic and therefore only infinite on a philosophical level.

Funny when you realise that JS' infinity is actually, at its most basic level, a very much finite amount of ones and zeroes.

Collapse
 
kitanga_nday profile image
Kitanga Nday

Second I saw that isNaN example I knew I had to check comments. IsNaN is very annoying to work with.

Collapse
 
matborowiak profile image
Mat Borowiak
  1. Math is a global object
  2. Class is a function, there are no classes in JavaScript, it is a syntatic sugar over native functions, which are virtualised through objects.
  3. See what Date constructor returns.

What you are implying is ridiculous to anyone who knows JavaScript. You clearly don't, so I am not sure what makes you confident to throw falsy claims about JS "lying". You didn't even check the language spec, and have no clue how this works.

developer.mozilla.org/en-US/docs/W...
developer.mozilla.org/en-US/docs/W...
developer.mozilla.org/en-US/docs/W...

Collapse
 
pcjmfranken profile image
Peter Franken • Edited

You misunderstand. I merely listed some common pitfalls that newcomers often seem to run into, and provided a link to further details.

What a hateful and abusive response. Completely uncalled for and gained you nothing.
I wish you a more joyful and happy day tomorrow.

Collapse
 
bitcreep profile image
Bitcreep

Everything besides null , undefined , strings, numbers, boolean, and symbols are objects in javascript.

Collapse
 
jimmonte profile image
jimmonte

There are also BigInts

Collapse
 
uploadcare profile image
Uploadcare

The fact that NaN is a number is totally fine and is not related to the JS. It's due to IEEE-754.

However, isNaN is broken, that's true.

Read more:

Collapse
 
pcjmfranken profile image
Peter Franken

That is a very solid and comprehensive article! Whilst not new knowledge to me, I believe people who might not be as familiar with this subject should have no problems following along!

Thanks for sharing it here

Collapse
 
wiseai profile image
Mahmoud Harmouch • Edited

That's just the tip of the iceberg:

NaN == NaN // false (Lol), so it seems like these NaNs are two different instances of a NaN object?
NaN instanceof Number // false, even though it is part of IEEE 754 numeric standard.
NaN == Number.NaN // false (lol), two different NaNs in JS? Probably like the first case (two different instances).
parseInt(NaN) // NaN (lol)
Infinity == Infinity // true (lol), like how?
Enter fullscreen mode Exit fullscreen mode
Collapse
 
bsides profile image
Rafael Pereira

You're treating NaN as a constant, which is not. Makes a lot of sense once you understand what NaN is. When you do an operation that results in NaN, its failure is not exactly the same as the other. Imagine doing an operation manually and you'll understand that it's never equals the other and there are more ways to reach that result, making it always different. This blog post is good to know more: ntgard.medium.com/why-nan-nan-3d41...

Infinity is not reverse NaN, they are pretty different. Infinity behaves as a property (or constant), which is the highest number (or lowest in case of negative infinity).

Infinity > Infinity // false
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
wiseai profile image
Mahmoud Harmouch • Edited

You're treating NaN as a constant, which is not.

I am literally using instanceof, which means not a constant, I guess?

Makes a lot of sense once you understand what NaN is.

doubt

When you do an operation that results in NaN, its failure is not exactly the same as the other. Imagine doing an operation manually and you'll understand that it's never equals the other and there are more ways to reach that result, making it always different.

Why are you explaining it like it is magic?

Infinity is not reverse NaN, they are pretty different.

Of course. But, I mean does it make more sense Infinity == Infinity is true and NaN == NaN is not?

Thread Thread
 
bsides profile image
Rafael Pereira • Edited

I am literally using instanceof, which means not a constant, I guess?

Great, then why the comparison? Just because a instanceof can be equal to the other doesn't mean all of them are equal, right?

Why are explaining it like it is magic?

I tried to explain because I thought it would help. A lot of times when I was learning JavaScript and came into something like this, an explanation which was not complex as say IEEE754 really helped me. So if it sounded condescending I'm sorry, it was not my intention.

Of course. But, I mean does it make more sense Infinity == Infinity is true and NaN == NaN is not?

At first yes, for sure. That's why it's important to see how they got to this point when they were developing the language. It's not a bug if it's intentional and I was very surprised with most of them to be intentional when I was learning. I mean, it's part of the process to start liking it, otherwise people tend to be stuck in "this language is trash because of this", you know? At least happened to me.

Thread Thread
 
wiseai profile image
Mahmoud Harmouch

A lot of times when I was learning JavaScript and came into something like this, an explanation which was not complex as say IEEE754 really helped me.

But, my concern is that there is a NaN object under the Number type (Number.NaN) even though NaN is NOT a number - I think the reason for this is that NaN is part of the IEEE 754 values which means all Number values including NaN. So, the thing that is confusing is the naming of these variables. Same thing for Infinity which is not actually the usual mathematical infinity, but rather a const (max value):

parseFloat("Infinity") === Infinity // true
parseFloat("NaN") === NaN // false
Enter fullscreen mode Exit fullscreen mode

At first yes, for sure. That's why it's important to see how they got to this point when they were developing the language. It's not a bug if it's intentional and I was very surprised with most of them to be intentional when I was learning. I mean, it's part of the process to start liking it, otherwise people tend to be stuck in "this language is trash because of this", you know? At least happened to me.

Yeah, i see.

Thank you for sharing your knowledge and experience.