New course! Every coder should learn Generative AI!
Try a free lesson+ 7
I thought 0 in boolean expression is true but here, the code gives an error, why? And why is the output of the second program 0?
//first program var value = 0; if(value) { return true; } else { return false; } //second program var count = 0; for(var i=0; i<3; i++) { for(var j=0; j<=2; j++) { continue; count++; } } document.write(count);
8 Answers
+ 6
0 is usually false.
+ 5
1. "The Boolean value of 0 (zero), null, undefined, empy string is False".
https://www.sololearn.com/learn/JavaScript/1129/
2. The "continue" statement breaks one iteration in the loop and continues with the next iteration.
In other words, "count++;" is always skipped.
+ 4
Diego so js is kinda like Ruby, Ruby treats every object except false and nil as true..yeah i think so it goes
+ 4
Diego, I still don't get why it gives 0
+ 3
Mofey At the beginning, "count" equals 0. Since "count++;" is always skipped, its value remains as 0.
+ 3
Diego what if "break;" was put in place "continue;" will the output still be 0? And also, I still don't understand why the output of the first program throws an error and not either false or true
+ 2
this does not apply in javascript, values for boolean must be true or false. btw 0 means false in the other languages like c,c++ etc not true
0
Yes