Suhail Kakar
Suhail Kakar

@SuhailKakar

6 Tweets 18 reads Jan 12, 2023
In JavaScript,
1 + "1" = 11
But,
1 - "1" = 0
Here is why ↓
→ We all know that 1 + 1 = 2, and 1-1=0 but how does the JavaScript Engine compute give the first answer 11 and the second answer 0.
→ In computer programming, Concatenation, in the context of programming, is the operation of joining two strings together. The term"concatenation" literally means to merge two things together.
→ Similarly in JavaScript, The + is a concatenation operator for strings. Therefore the number is subsequently transformed into a string and concatenated as a result.
→ To make it more clear, The + operator in Javascript (as in many other languages) has two different Jobs: It acts as an addition operator, and as a string concatenation operator. In our case, the concatenation takes precedence over the addition of numbers.
→ However, the 2nd case is called concatenation. The "-" is only for numeric, both operands are converted to numeric and thus give a numeric answer.

Loading suggestions...