I am puzzled by this snippet:
var n1 = 5-"4";
var n2 = 5+"4";
alert(n1);
alert(n2);
I understand that n1 is 1. That is because a minus operator would convert the string “4” into number and subtract it from 5.
But why do we get 54 in case of + operator?
Can someone explain this difference between + and = operators to me?
By type conversion any
+expression, that contains a strings, will result in a string. Thus all operands (in your case5) will be converted to a string, before executing the concatenation.-on the other hand is just an arithmetic operand, thus"4"is converted to an integer and the calculation is performed as you expect.