+ 3
In Javascript ("11" + 1 = 111) and ("11" - 1 = 10). Why? đ
JavaScript
4 Answers
+ 3
The - operator is only used for number's so it will convert the left hand side and right hand side to numbers and subtract.
The + operator is used for string concatenation and addition for numbers. If either the lhs or rhs is a string the other operand gets converted to a string (it will be used as a string concatenation)
+ 1
JavaScript is a weakly typed language, meaning that when an operation is called on two types that don't match, the compiler will silently attempt to cast one of the values to another type in order to succeed with the operation.
Thus, in your example when using the '+' operator, the compiler puts precedence on casting to strings and concatenating; when you use the '-' operator, the compiler attempts to cast into a number to get a valid result.
Hope this gives you some insight âïž
0
đđđđ Great !!!.