Hacker News new | ask | show | jobs
by ndefinite 5174 days ago
It's the same with JavaScript regardless of whether you use == or ===

9223372036854775807 == 9223372036854775808

true

9223372036854775807 === 9223372036854775808

true

5 comments

Perhaps those two lines are the same in JS and PHP (I do not have PHP installed so I can't confirm, but I do have node installed):

    > 9223372036854775807 == 9223372036854775808
    true
    > 9223372036854775807 === 9223372036854775808
    true
However, that is not the two lines written about in this post. The following works as one would expect on JS, but (I assume, based on the report) not in PHP:

    > "9223372036854775807" == "9223372036854775808"
    false
    > "9223372036854775807" === "9223372036854775808"
    false
ah, I see. I missed that. I just checked in chrome's console and see false for both when they're strings.
The difference being that the php code sample is trying to compare two strings.
While this comment is beside the point, I'm still curious why this version works the way it does. I may be missing something obvious, but why does JS think those two integers are the same?
Probably due to implicit conversion to floating point format. After conversion, mantissa and exponent are the same, and least significant bits are truncated during conversion.
JavaScript doesn't have an fixed integer data type.
Is "javascript does it too" supposed to be a defence?
More of an observation than anything else. I don't actually use PHP but find these recent PHP focused articles are helping me learn at thing or two about languages I do use.
Fair enough.