3

What does the following evaluate to?

"1"+2+4

What about this:

5 + 4 + "3"

In the first case since "1" is a string, everything is a string, so the result is "124". In the second case, its 93.what is happening here? Why does addition happen in one instance, while string concatenation occurs in the other?

var x = "1" + 2 + 4;
var z = 5 + 4 + "3";
console.log(x); // "124"
console.log(z); // 93 

Can anyone explain this?

5
  • @MitchWheat and what about the first one?According to u in the first case can i do this:"1"+6.It will be wrong Commented Feb 5, 2013 at 5:02
  • 1 + "" (converts number to string) Commented Feb 5, 2013 at 5:02
  • 1
    What's happening - (a) left-to-right operator precedence, and (b) automatic type conversion. Commented Feb 5, 2013 at 5:04
  • In the second one, because the numbers are first they get added before being converted into a string by the "3". In the first one "1" + 2 gets done first. Which results in a string of "12"..so then it just adds 4 to that string resulting in "124" Commented Feb 5, 2013 at 5:05
  • 2
    You may read this to learn operator precedence (developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/…) Commented Feb 5, 2013 at 5:10

5 Answers 5

5

expression evaluates from left to right.

"1"+2+3
 ^--^
"12" //string  +3
  ^_____________^
 "123"  //string

in 2nd case

 1+2+"3"
 ^_^
  3+"3"
  ^___^
  "33" // string
Sign up to request clarification or add additional context in comments.

Comments

1

Think about the operation order (rtl or ltr) each time it performs a binary operation it converts it accordingly so 5+4 will be int and (5+4) + "3" will be a string because "3" is a string

Same method applies to different examples

Comments

0

var x = "1" + 2 + 4; // 124

This is taking the string "1" and concatenating to it "2" and "4" as strings.

var z = 5 + 4 + "3"; // 93

This is taking the numbers 4 and 5 and adding them together to get the number 9, and then concatenating the string "3" to that to produce another string.

The key thing to take away here is that the end result of what you're doing here is string concatenation. The order of evaluating the numbers is different but the end result is a string.

Comments

0

In the first case you create a string first (1) and then javascript concatenates the following number as strings (124).

In the second one you create a number first then javascript adds the second number to this first number (5 + 4 = 9) and then you add a string so it does the concatenation of 9 and 3

Comments

0

In both case apply the type conversion and left to right precedence. in first one,

var x = "1" + 2 + 4; // 124

compiler take 1 as string and after that it will concatenating with 2 now 12 is the string so it will concatenate with 4 and result will produce "124" as string.

var z = 5 + 4 + "3"; // 93

in Second one, first 5 and 4 is numeric so make addition and result will be 9. and this will concatenate with string 3 soo output will be 93 as string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.