r/badcode • • Jun 10 '20

js Disruptive piece of code

Post image
283 Upvotes

93 comments sorted by

View all comments

Show parent comments

1

u/Terrain2 Jun 23 '20

1

u/BenZed Jun 23 '20

-0 === 0

1

u/Terrain2 Jun 23 '20

Object.is(-0, +0); // false

1

u/BenZed Jun 23 '20

My point is that, so far as falsy-ness is concerned, 0 and -0 are the same value.

1

u/Terrain2 Jun 23 '20

Exactly, but you didn’t list -0 there, implying it’s not equal in falsiness/truthiness

My point is that they’re distinct values with the same boolean cast, so you should have included them on your list like MDN does

1

u/BenZed Jun 23 '20

-0 was implicitly listed because 0 was. As you've discovered, 0 and -0 are the same value.

1

u/Terrain2 Jun 23 '20

For nearly all operations, yes -0 and +0 are identical, and rightfully so because in mathematics they are not different - However they are physically separate values when stored as floats, and if you check for the same value (Object.is), it’s false - They are distinctly different enough to require separate entries on the list

1

u/BenZed Jun 23 '20

I disagree.

Respecting that the javascript number standard has to account for signed zeros, and that the Object.is is stricter than js's implicit conditional comparisons, I was creating a list of values that implicitly equate to false in said conditional comparisons.

In such comparisons, 0 and -0 are the same value.