0

why the answer is 0? thanks in advance

var x=1; if(x){ x=0; } if(x){ x=1; } document.write(x);

14th Sep 2016, 11:13 PM
muhammad aqmal
muhammad aqmal - avatar
3 Respuestas
+ 4
0 is false and any non-zero value is true.
14th Sep 2016, 11:38 PM
Zen
Zen - avatar
+ 3
var x=1; //x is now 1 if(x){ // validates condition x=0; //now x is 0 } if(x){ //fails condition x=1; } document.write(x); //prints 0 That's it.
15th Sep 2016, 8:09 AM
Nagendra Prajwal
Nagendra Prajwal - avatar
+ 1
Any non-zero number is True whereas zero is False. Since, x=1 at the first if the condition is True, and it is executed, which makes the value of x=0. Now, the second if is not executed since x becomes 0, and hence the condition is false. That is why the answer is zero.
16th Sep 2016, 6:53 PM
Mayank Chauhan
Mayank Chauhan - avatar