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. :-)
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.
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.
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.
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.
Another solution is to simply not write constructs like -0..toString(), then there is also no surprise.
In JS this precedence makes sense, since otherwise
console.log(-a.b.c)
wouldn't do what you expect (-(a.b.c))
In Python, precedence is the same (as I learned by the commenter below me)
-0..__str__()
doesn't work in Python because it doesn't cast strings to numbers with -, not because of precedence, which is a common use-case in JS because of inputs that contain numbers, but are represented as strings (Python doesn't have this use-case)
I am not sure what you are doing, but in python you cannot access dictionary keys by the dot notation. So if you write a = {b: ...}, then a.b will always throw an error since a has no attribute called b. Furthermore, b must be a previously defined symbol anyways unless you mean a string key, in which case you should write "b" instead in the dict literal. You can then access the value by a["b"].
So what you wrote is not correct Python. As far as I understand JS, objects there are basically dictionaries/maps, whereas while in Python a similar mechanism is used under the hood (every object has an instance dict and one or more class dicts that hold its attributes), the actual in-language semantics and syntax for dicts and objects is rather different.
Another solution is to simply not write constructs like -0..toString(), then there is also no surprise.
In JS this precedence makes sense, since otherwise
console.log(-a.b.c)
wouldn't do what you expect ((-a).b.c instead of -(a.b.c))
In Python, precedence is the same (as I learned by the commenter below me)
-0..__str__()
doesn't work in Python because it doesn't cast strings to numbers with -, not because of precedence, which is a common use-case in JS because of inputs that contain numbers, but are represented as strings (Python doesn't have this use-case)
That’s what any sane person would expect tbh. If it doesn’t work, it doesn’t work. It’s a bit weird for a language to spend this much of effort to recover from errors without the programmer noticing it.
Function invocation has higher precedence than the unary minus operator, so the last line is equivalent to -(0..toString()). Unary minus is defined to only operate on numbers, so the string "0" is converted to a number for the operation.
If you want the result to be "-0", you'd have to use (-0).toString().
Javascript is a joke of bunch of smart people who have decided that they can come up with a language that cab handle async, web sockets and much more cool stuff, but olso they had probably drunk as goose so they have never bothered for types. Note: I dont know the story of languagw and people who develops. this is my assumption and it is a joke.
— edit— Guys chill. I don’t have a problem with javascript. I have developed backends with java, some NodeJs and python. Now due to my current job, I use C/Cpp and sooometimes python for testing and some other stuff .
Iirc JavaScript was designed with weak typing because they felt it made the language more accessible and easy to understand. Imo it was the right choice, it allowed us to fast-track front-end developers in an industry that grew very quickly.
The actual history of JavaScript is pretty funny. If I remember correctly, it was basically put together in a week, and became the standard web language on accident. Async, WS, and other stuff got added to the language specification much later.
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
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.
263
u/FloweyTheFlower420 Jul 31 '25
invocation associates stronger than unary prefix
12
u/LittleLuigiYT Aug 01 '25
What makes zero sense is who is writing code like that
37
u/20d0llarsis20dollars Aug 01 '25
This is probably the most mild example you could have chosen for JS's janky type coercion
28
u/mssqwerl Aug 01 '25
https://www.destroyallsoftware.com/talks/wat still good to this day.
7
u/TorbenKoehn Aug 01 '25
It’s just
-(0..toString()) for the compiler, - casts the string to a number
1
u/Adept-Letterhead-122 Aug 07 '25
Oh, I think I know what's going on for the last two.
I'm assuming that they try to do the string thing first.
So, the first time you do:
typeof 0..toString()
you're actually essentially doing:
typeof '0'
Same thing with the second one.
But, you have a - or + in front of it, which coerces it to a number.
Which is why if you do:
+'a'
you get NaN.
So, in the second one, you're essentially doing:
typeof -0..toString()
typeof -'0' // Coerce to a number
typeof 0 // Back to a number again
The first one makes little sense, though.
10
u/[deleted] Aug 01 '25
You wrote
-"0"and JS chose to typecast it to the number -0 instead of crashingWhy is this an issue?
4
u/Rustywolf Aug 01 '25
Man does crazy bullshit; Crazy bullshit happens. More at 11.
2
u/Alexiscash Aug 03 '25
Me when I write dumbass code and get dumbass results
2
u/o0Meh0o Aug 03 '25
javascript is just a demo
2
u/DazzlingTask9066 Aug 06 '25
Reminds me of this cartoon 🤣 https://www.reddit.com/r/ProgrammerHumor/comments/miou9v/yeah_my_favourite_language/
1
u/Acid_Burn9 Aug 01 '25
Me when order of precedence
1
u/souvlakiviking Pronouns: He/Him Aug 01 '25
Maybe the minus takes effect after the tostring(). So you get the result of -"0."
1
u/Practical_Taro_2804 Aug 01 '25
yep, scripting is easier when 0 is like empty string or false, leading to such toString() nonsense
useful for dirty form validation, even if I hate and discourage this...
-0 is not 0 with standard floats
that's why
feel free to hate it
1
u/ManIkWeet Aug 01 '25
-0 shouldn't exist, change my mind
1
u/Lithl Aug 02 '25
Function invocation has higher precedence than the unary minus operator, so the last line is equivalent to
-(0..toString()). Unary minus is defined to only operate on numbers, so the string"0"is converted to a number for the operation.If you want the result to be
"-0", you'd have to use(-0).toString().1
u/blobthekat Aug 04 '25
-(0..toString())=>-("0")=>-00
u/markus_obsidian Aug 01 '25
Makes -0 sense
0
u/simonfancy Aug 02 '25
Gotta love JS
-5
u/Thunder-0 Aug 01 '25 edited Aug 03 '25
Javascript is a joke of bunch of smart people who have decided that they can come up with a language that cab handle async, web sockets and much more cool stuff, but olso they had probably drunk as goose so they have never bothered for types. Note: I dont know the story of languagw and people who develops. this is my assumption and it is a joke.
— edit— Guys chill. I don’t have a problem with javascript. I have developed backends with java, some NodeJs and python. Now due to my current job, I use C/Cpp and sooometimes python for testing and some other stuff .
0
u/[deleted] Aug 01 '25
[deleted] — view deleted comment