Can anyone please explain this weird javascript behavior?
+
// Rightfully gets a syntax error, because there are no operands
+ +
// Same as above
+ + ''
// Interpreted as 0
typeof(+ + '')
// Interpreted as "number"
+ + '' === 0
// Interpreted as true
So in other words, (+ + '') is computed as zero. 1) How is that even syntactically allowed? 2) Why does that compute to zero?
The unary plus (+) operator converts the right hand side to a number.
So you have 0 ← 0 ← empty string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With