+ 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?

23rd Dec 2016, 10:40 AM
Farid Zamanov
Farid Zamanov - avatar
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
23rd Dec 2016, 11:02 AM
visph
visph - avatar
+ 4
Booleans: True != 0, False = 0 The "if" conditions are satisfied because statements return true so last value of x will be 1.
23rd Dec 2016, 10:51 AM
Valen.H. ~
Valen.H. ~ - avatar
+ 4
Oops! yes, typo mistake (corrected)....
23rd Dec 2016, 11:33 AM
Valen.H. ~
Valen.H. ~ - avatar