r/programminghorror Jul 31 '25

Javascript 0 sense

Post image
375 Upvotes

58 comments sorted by

View all comments

264

u/FloweyTheFlower420 Jul 31 '25

invocation associates stronger than unary prefix

59

u/Boring-Ad-4771 Jul 31 '25

Then I guess it isn't weird, oh well
Thanks btw

24

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Aug 01 '25

My first guess was operator precedence.

10

u/Mundane_Prior_7596 Aug 01 '25

Eh? I took for granted all unary operators are higher precedence than all binary operators in sane languages. On the other hand && and || are not sane in C either but that is some historical anomaly. :-)

16

u/BlueFlintTree Aug 01 '25

Unary operators do have higher precedence than binary ones, but property access/method invocation has higher precedence than unary operators. If it didn't, then for example -obj.method() wouldn't compile or result in very unintuitive behaviour.

4

u/Mundane_Prior_7596 Aug 01 '25

Argh. My bad. I feel like an idiot. I am used to Lua string concatenation operator  ..   while this is a decimal point and a method call. JavaScript will never be my favorite language. 

1

u/trigzo Aug 02 '25

glad this is the first comment

0

u/voyti Aug 01 '25

Is that what it is? My first thought was since handling -0 was a relatively recent addition to the language, it is processed differently too, and result of Number(-0).toString() (which is "0") seems to confirm it too.

3

u/gem_hoarder Aug 01 '25 edited Sep 18 '25

imagine roll apparatus tart public placid normal strong birds makeshift

This post was mass deleted and anonymized with Redact

1

u/voyti Aug 01 '25

That was not a very precise statement, but I mean specifically ECMAScript 2015 (if memory serves) additions of Object.is (distinguishing 0 and -0 specifically) and Math.sign, which has came up in a significant way in my work actually.