What will be the value of the c variable after executing the code:
a = "5"; b = 2; c = a+++b; 
Unary operator (++) has higher precedence than addition:
a +++ b is equivalent to (a ++) + b.
var = 5;
a++ = 5 // post increment
b = 2;
c = 5 + 2 // 7
Read more: Operator Precedence (JavaScript)

The explanation is not correct. It is not a problem of precedence. Yes `++` operator has precedence over `+`. But here, it is the Left to Right order that matter. First, `var a` is evaluated, and there is an automatic conversion from string to number : a is 5 on the Left side of the addition. Then `a` is incremented. Second, the left side is evaluated. So, on the left side of the addition: 2. The addition itself is processed : 5 + 2 => 7. You have to make the difference between the evaluation of each side of the addition and the re-assignement of the `var a` which takes place after because it is a suffix increment and not a prefix one.

2018 Aug 26, 3:37:13 PM

@misterds cool explanation!

2018 Jan 30, 5:05:32 PM

let str = "5"; undefined typeof str; "string" str++ 5 typeof str "number"

2017 Dec 15, 5:05:37 PM

Hi I check for me and i made something in console this it will be useful for you to understanding

2017 Dec 15, 5:05:26 PM

@aalaminosz26 thanks! updated explanation, added "read more" section. While applying unary operator it automatically converts to the number. Read more here: https://scotch.io/tutorials/javascript-unary-operators-simple-and-useful

2017 Dec 5, 3:58:48 PM

Also it is not clear when is the string "5" converted into numeral 5?

2017 Nov 25, 8:10:58 AM

I find the explanation confusing. Maybe the fact that it is all in one line.

2017 Nov 25, 8:09:15 AM

@iliana000, we repeat questions intentionally. Soon there will be more questions, and functionality will train you on sub-topics, that you answered wrong before.

2016 Jan 20, 5:49:31 PM

I see this question for the second time while one test

2016 Jan 15, 7:21:23 AM

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback