Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

string concatenation and addition

When I try to do addition:

let x = "5" + 2 + 3;
//output 523

let x = 2 + 3 + "5";
// output 55

I know JavaScript concatenates the integers, but I was expecting "55" in both cases.

As 2+3 will be added to 5 and then concatenated to "5". Please can someone explain to me
what is happening under the hood.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I am new to JavaScript.

>Solution :

The pluses are evaluated from left to right.
So first "5"+2 is evaluated (result is "52"), and then "52"+3 gives "523".

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading