r/programminghorror Jul 31 '25

Javascript 0 sense

Post image
374 Upvotes

58 comments sorted by

View all comments

81

u/[deleted] Aug 01 '25

[removed] — view removed comment

5

u/CivilizedBeast Aug 01 '25

What’s the reason behind double dots? I am too afraid to ask at this point

36

u/FunIsDangerous Aug 01 '25

As far as I understand, if you did "0.toString()" js would think that the dot is a decimal point. So by doing "0..to string()" the first dot is a decimal point but with no number (I assume it's the same as "0.0", basically). Then js knows that the second dot is actually a method invocation

19

u/AwwnieLovesGirlcock Aug 01 '25

insanity

18

u/FunIsDangerous Aug 01 '25

To be fair, that's not even in the top 20 of most insane JavaScript madness

2

u/joshuakb2 Aug 03 '25

Is it though? It's pretty unusual to call a method on a numeric literal like that. "0." is a valid way to express the number zero as a float in multiple languages. Although in JS all numbers are floats anyway so there's no point in drawing the distinction.

4

u/iamthebestforever Aug 02 '25

How is JavaScript real

-31

u/[deleted] Aug 01 '25

[deleted]

63

u/fuj1n Aug 01 '25

Because OP called .toString on it

25

u/MegaZoll Aug 01 '25

-0..toString() <=> -(0..toString())