I read the following on a question on SO:
‘7’ + 4 gives ’74’, whereas ‘7’ – 4 gives 3 in JavaScript
Why does this happen?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The ‘+’ operator is defined for both strings and numbers, so when you apply it to a string and a number, the number will be converted so string, then the strings will be concatenated:
‘7’ + 4 => ‘7’ + ‘4’ => ’74’
But ‘-‘ is only defined for numbers, not strings, so the string ‘7’ will be converted to number:
‘7’ – 4 => 7 – 4 => 3