Confusion JavaScript String Statement Concatenation

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 does not fall and x = NaN. Now that I have fixed this error, I am wondering 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 someone explain to me what is going on in the statement and how nothing (the space between the + signs) changes this from error to NaN?

PS. I'm not sure if this should go here or programers.stackoverflow.com. Let me know if I have posted on the wrong site.

+3


source to share


1 answer


This is interpreted as follows:

var x = "hi" + (+"mom")

      



The prefix +

tries to force the string to a number. Number('mom')

- NaN

, therefore +'mom'

also NaN

.

+7


source







All Articles