items = 3;
$('#div').html(items + 1 + ' - ' + items + 3);
Trying to make #div display 4 - 6, but it instead shows 4 - 33. What am I missing?
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.
As to why it happened:
first the parser reads
items + 1, so fine and dandy, it’s4then it concatenates with
' - ', so now you have4 -then it sees
+ items, at this moment, you are dealing withString, so it concatenates with 3 (asitemsis 3), so you have4 - 3then you have another concatenation with
3, but the left operand is astring, so the right operand is type casted intoStringas well, so you have4 - 33To achieve what you want, you need to enclose the inner operations: