Why is “5”- 5 = 0 but “5” + 5 = “55” in JavaScript?

Let’s understand this weird behavior in JavaScript

The arithmetic “+” operator

The binary + operator is generally used to add two numbers, when both of the operands are just numbers it will give the sum(quite obvious)

But when one of the operands is a string then the + acts as a string concatenation operator.

The other operand if it is a number, it will be converted into a string.

2+2=4
2+"2"="22"
"2"+2="22"
"2"+"2"="22"

The arithmetic “ — “ operator

The – operator only operates with numbers only.

If both operators are numbers, it will do the casual subtraction. If the operators are numbers represented in strings, it will just convert those into numbers.

If there exist any character strings like “hello” , it will just result in NaN.

2-5=-3
5-4=1
"500"-100=400
"50"-"19"=31
"adarsh"-9=NAN

Precedence of “+” and “-” operator

We know the BODMAS rule from our high school, here addition and subtraction got the same priority or precedence.

Hence it will evaluate from left to right.

"100"+200-"150"  //will start from this
"100"+"200"-"150"
"100200"-"2"
100200-2
100198 //will end like this

Unlike many other high-level languages out there, JavaScript is a dynamically typed language and is not strict about the datatypes of the operand.

When the operands are of different types it will convert them into suitable types instead of showing errors

These cannot be called a weirdness or a feature of JavaScript. These are just how JavaScript works under the hood.

In simple word + is both a concatenation as well as an addition operator. If any operands/arguments are not m=numeric it will do concatenation, that’s why “2”+”2″ == 22 just like “adarsh”+” gupta” == “adarsh gupta”.

– is only subtraction. No matter what its arguments are, they are coerced to numbers. Thus, ’22’ – ‘2’ is evaluated as 22 – 2 which is 20.

Conclusion

We have learned why the + operator does both concatenation and addition in different scenarios.

I hope you have found these useful. Be sure to follow me on Twitter as I’m active there too.

Build composable web applications

Don’t build web monoliths. Use Bit to create and compose decoupled software components — in your favourite frameworks like React or Node. Build scalable and modular applications with a powerful and enjoyable dev experience.

Bring your team to Bit Cloud to host and collaborate on components together, and speed up, scale, and standardize development as a team. Try composable frontends with a Design System or Micro Frontends, or explore the composable backend with serverside components.

Give it a try →

Learn more


Why is “5”- 5 = 0 but “5” + 5 = “55” in JavaScript? was originally published in Bits and Pieces on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Bits and Pieces - Medium and was authored by Adarsh gupta

Let’s understand this weird behavior in JavaScript

The arithmetic "+" operator

The binary + operator is generally used to add two numbers, when both of the operands are just numbers it will give the sum(quite obvious)

But when one of the operands is a string then the + acts as a string concatenation operator.

The other operand if it is a number, it will be converted into a string.

2+2=4
2+"2"="22"
"2"+2="22"
"2"+"2"="22"

The arithmetic “ — “ operator

The - operator only operates with numbers only.

If both operators are numbers, it will do the casual subtraction. If the operators are numbers represented in strings, it will just convert those into numbers.

If there exist any character strings like "hello" , it will just result in NaN.

2-5=-3
5-4=1
"500"-100=400
"50"-"19"=31
"adarsh"-9=NAN

Precedence of “+” and “-” operator

We know the BODMAS rule from our high school, here addition and subtraction got the same priority or precedence.

Hence it will evaluate from left to right.

"100"+200-"150"  //will start from this
"100"+"200"-"150"
"100200"-"2"
100200-2
100198 //will end like this

Unlike many other high-level languages out there, JavaScript is a dynamically typed language and is not strict about the datatypes of the operand.

When the operands are of different types it will convert them into suitable types instead of showing errors

These cannot be called a weirdness or a feature of JavaScript. These are just how JavaScript works under the hood.

In simple word + is both a concatenation as well as an addition operator. If any operands/arguments are not m=numeric it will do concatenation, that's why "2"+"2" == 22 just like "adarsh”+" gupta" == “adarsh gupta”.

- is only subtraction. No matter what its arguments are, they are coerced to numbers. Thus, '22' - '2' is evaluated as 22 - 2 which is 20.

Conclusion

We have learned why the + operator does both concatenation and addition in different scenarios.

I hope you have found these useful. Be sure to follow me on Twitter as I’m active there too.

Build composable web applications

Don’t build web monoliths. Use Bit to create and compose decoupled software components — in your favourite frameworks like React or Node. Build scalable and modular applications with a powerful and enjoyable dev experience.

Bring your team to Bit Cloud to host and collaborate on components together, and speed up, scale, and standardize development as a team. Try composable frontends with a Design System or Micro Frontends, or explore the composable backend with serverside components.

Give it a try →

Learn more


Why is “5”- 5 = 0 but “5” + 5 = “55” in JavaScript? was originally published in Bits and Pieces on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Bits and Pieces - Medium and was authored by Adarsh gupta


Print Share Comment Cite Upload Translate Updates
APA

Adarsh gupta | Sciencx (2022-05-09T10:01:48+00:00) Why is “5”- 5 = 0 but “5” + 5 = “55” in JavaScript?. Retrieved from https://www.scien.cx/2022/05/09/why-is-5-5-0-but-5-5-55-in-javascript/

MLA
" » Why is “5”- 5 = 0 but “5” + 5 = “55” in JavaScript?." Adarsh gupta | Sciencx - Monday May 9, 2022, https://www.scien.cx/2022/05/09/why-is-5-5-0-but-5-5-55-in-javascript/
HARVARD
Adarsh gupta | Sciencx Monday May 9, 2022 » Why is “5”- 5 = 0 but “5” + 5 = “55” in JavaScript?., viewed ,<https://www.scien.cx/2022/05/09/why-is-5-5-0-but-5-5-55-in-javascript/>
VANCOUVER
Adarsh gupta | Sciencx - » Why is “5”- 5 = 0 but “5” + 5 = “55” in JavaScript?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/05/09/why-is-5-5-0-but-5-5-55-in-javascript/
CHICAGO
" » Why is “5”- 5 = 0 but “5” + 5 = “55” in JavaScript?." Adarsh gupta | Sciencx - Accessed . https://www.scien.cx/2022/05/09/why-is-5-5-0-but-5-5-55-in-javascript/
IEEE
" » Why is “5”- 5 = 0 but “5” + 5 = “55” in JavaScript?." Adarsh gupta | Sciencx [Online]. Available: https://www.scien.cx/2022/05/09/why-is-5-5-0-but-5-5-55-in-javascript/. [Accessed: ]
rf:citation
» Why is “5”- 5 = 0 but “5” + 5 = “55” in JavaScript? | Adarsh gupta | Sciencx | https://www.scien.cx/2022/05/09/why-is-5-5-0-but-5-5-55-in-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.