4

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

 2 years ago
source link: https://blog.bitsrc.io/in-javascript-5-5-0-but-5-5-55-but-why-b0629cd7de00
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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

Let’s understand this weird behavior in JavaScript

0*p98I41ZqhnSbXWvJ

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=42+"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=-35-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-2100198 //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 →

0*YpHFTWA6pG6xcuhs.gif

Learn more


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK