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...
Frontend Web Developer -
Programming Content Creator -
60k+ Followers On Linkedin -
Open To Opportunities -
Follow me for the most amazing content related to Programming & Web Development 🚀
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.
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.
Class is a function, there are no classes in JavaScript, it is a syntatic sugar over native functions, which are
virtualised through objects.
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.
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!
Nice to meet you, ma fren 🫡. Sorry, I ain't DEVing that much ✍️ , primarily due to the nature of maintaining Open Source projects 👷, while also gigging 💰. Anyways, stay humble like a bumblebee 🐝.
NaN==NaN// false (Lol), so it seems like these NaNs are two different instances of a NaN object?NaNinstanceofNumber// 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?
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).
Nice to meet you, ma fren 🫡. Sorry, I ain't DEVing that much ✍️ , primarily due to the nature of maintaining Open Source projects 👷, while also gigging 💰. Anyways, stay humble like a bumblebee 🐝.
I am literally using instanceof, which means not a constant, I guess?
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.
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?
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.
Nice to meet you, ma fren 🫡. Sorry, I ain't DEVing that much ✍️ , primarily due to the nature of maintaining Open Source projects 👷, while also gigging 💰. Anyways, stay humble like a bumblebee 🐝.
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):
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.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Careful, JS is effing weird:
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...Thanks for sharing 🙌
IMO infinity is not a number.
Yes
Well, in JS it is a number.
maybe calling it Infinity was a poor choice 😉
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
isFiniteglobal function (spec, MDN) might come in handy next time you need to validate a numerical type.Infinity is a number in math. I think it's set this way for internal reasons, but outside of programming infinity is a number
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.
Second I saw that isNaN example I knew I had to check comments. IsNaN is very annoying to work with.
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...
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.
Everything besides null , undefined , strings, numbers, boolean, and symbols are objects in javascript.
There are also BigInts
The fact that NaN is a number is totally fine and is not related to the JS. It's due to IEEE-754.
However,
isNaNis broken, that's true.Read more:
What is NaN in JavaScript?
Uploadcare ・ Jan 25 ・ 7 min read
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
That's just the tip of the iceberg:
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).
I am literally using
instanceof, which means not a constant, I guess?Why are you explaining it like it is magic?
Of course. But, I mean does it make more sense
Infinity == Infinityis true andNaN == NaNis not?Great, then why the comparison? Just because a instanceof can be equal to the other doesn't mean all of them are equal, right?
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.
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.
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):
Yeah, i see.
Thank you for sharing your knowledge and experience.