0
why the answer is 0? thanks in advance
var x=1; if(x){ x=0; } if(x){ x=1; } document.write(x);
3 ответов
+ 4
0 is false and any non-zero value is true.
+ 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.
+ 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.