var num = "1";
num = num+1; //Gives "11"
but
var num = "1";
num++; //Gives 2.
Why?
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.
Because the
+operator is also used for string concatenation.Javascript is a weakly typed language, so it’s more lenient about type conversions.
The
++operator used in your case is the postfix increment operator which only operates on numbers, so it acts as you expect:To give a hint that addition should take place, subtract by zero:
Or use the unary
+operator: