+ 1
Why in JavaScript "11" + 1 = 111 but "11" - 1 = 10 ?
Please explain this. Thanks.
3 odpowiedzi
+ 9
Ярослав Вернигора (Yaroslav Vernigora) Yep... and in this case, it's implicit coercion. 😉
Umesh Kushwaha These articles should be helpful in understanding Javascript Type Coercion:
https://hackernoon.com/understanding-js-coercion-ff5684475bfc
https://www.google.com/amp/s/www.freecodecamp.org/news/js-type-coercion-explained-27ba3d9a2839/amp/
+ 5
In Javascript, strings can be concatenated, so thats what the browser thinks you are doing and converts the 1 into the string when you add it
Strings can not be subtracted though, so the interpreter converts the string to an integer in the second one
Javascript is irritating this way
+ 2
In the first case, a string merge operation is performed, not an arithmetic operation. In the second case, a subtraction operation is performed. The interpreter tries to cast everything to the same type. Read it in the section about explicit and implicit conversion of data types