Determine the final value of the JavaScript variable x after
Determine the final value of the JavaScript variable x after the following sequence of statements have been executed.
var x = 1, y = 0;
x = x + y;
y = x - y;
x = x + 2 * y;
document.write(\"x = \" + x);
Solution
var x = 1, y = 0;
x = x + y; // 1
y = x - y; // 1
x = x + 2 * y; // 1 + 2 * 1 => 3
document.write(\"x = \" + x);
Answer is :
3
