I was developing a node.js site and I made a copy and paste error that resulted in the following line (simplified for this question):
var x = "hi" + + "mom"
It doesn't crash and x = NaN. Now that i have fixed this bug, I am curious what is going on here, since if I remove the space between the + signs I get an error (SyntaxError: invalid increment operand)
My Question is : Can some explain to me what is going on in the statement and how nothing (a space between the + signs) changes this from an error to a NaN?
PS. I am not sure if this should go here or programers.stackoverflow.com. Let me know if I posted on the wrong site.
It's being interpreted like this:
var x = "hi" + (+"mom")
The prefix + tries to coerce the string to a number. Number('mom') is NaN, so +'mom' is also NaN.
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