Last Updated: February 25, 2016
·
585
· koendewaele

Watch out for expressions involving numeric and string values with the + operator

in expressions involving numeric and string values with the + operator, JavaScript converts numeric values to strings.

For example, consider the following statements:

x = “The answer is ” + 42 // returns “The answer is 42”
y = 42 + ” is the answer” // returns “42 is the answer”

In statements involving other operators, JavaScript does not convert numeric values to strings. For example:

“37” - 7 // returns 30
“37” + 7 // returns 377