var a = 1;
var с = a+++1;
What will be the value of c variable after executing the code?
Actually, it is a quite tricky question. In this case correct answer is 2. With ++i, the value is incremented, then evaluated. So if i was 1, it would be evaluated as 2. With i++, if i was 1, it would be evaluated as 1, but then stored as 2. These statements both seem to mean the same thing, well, they do, if the expressions are used in a standalone fashion. However, it’s when they’re used as part of a larger statement, do the differences shine through. Lets consider evaluation steps for two cases: 1) var c = a+++1; => (a++)+1 => (1) + 1 => 2 2) var c = ++a+1; => (++a)+1 => (2) + 1 => 3
2015 Jul 3, 3:22:38 PM
Login in to like
Login in to comment