+ 1
javaScript
var x=1; if(x) {x=0;} if(x) {x=1;} document.write(x); i just learn Javascript. but i dont understood this exercise.after write in the editor i get some results but i dont understood logic. who can expalin this?
3 Answers
+ 5
@ValentinHacker:
Your answer disrupt me... Booleans are not ( True=1 and False=2 ) but 0 is equivalent to False, and other values equivalent to True, isn't it?
Well, this code:
document.write("test<br>");
for (var i=0; i<3; i++) {
if (i) {document.write(i+"<br>");};
}
output:
test
1
2
...
[ EDIT ]
and negative values also equivalent to True
+ 4
Booleans: True != 0, False = 0
The "if" conditions are satisfied because statements return true so last value of x will be 1.
+ 4
Oops! yes, typo mistake (corrected)....