Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does (+ + '') === 0?

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?

like image 258
carlbenson Avatar asked Mar 12 '26 06:03

carlbenson


1 Answers

The unary plus (+) operator converts the right hand side to a number.

So you have 0 ← 0 ← empty string.

like image 151
Quentin Avatar answered Mar 13 '26 20:03

Quentin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!