How does adding String with Integer work in JavaScript? [duplicate]

"" + 1          === "1"
"1" + 10        === "110"
"110" + 2       === "1102"
"1102" - 5      === 1097
1097 + "8"      === "10978"

In JavaScript, the + operator is used for both numeric addition and string concatenation. When you “add” a number to a string the interpreter converts your number to a string and concatenates both together.

When you use the - operator, however, the string is converted back into a number so that numeric subtraction may occur.

When you then “add” a string "8", string concatenation occurs again. The number 1097 is converted into the string "1097", and then joined with "8".

Leave a Comment