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? | Sololearn: Learn to code for FREE!
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);

8th Jul 2019, 4:56 AM
eMBee
eMBee - avatar
8 Answers
+ 6
0 is usually false.
8th Jul 2019, 6:01 AM
Sonic
Sonic - avatar
+ 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.
8th Jul 2019, 5:08 AM
Diego
Diego - avatar
+ 4
Diego so js is kinda like Ruby, Ruby treats every object except false and nil as true..yeah i think so it goes
8th Jul 2019, 5:12 AM
Mind To Machine 💻🕆
Mind To Machine 💻🕆 - avatar
+ 4
Diego, I still don't get why it gives 0
8th Jul 2019, 5:12 AM
eMBee
eMBee - avatar
+ 3
Mofey At the beginning, "count" equals 0. Since "count++;" is always skipped, its value remains as 0.
8th Jul 2019, 5:16 AM
Diego
Diego - avatar
+ 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
8th Jul 2019, 5:33 AM
eMBee
eMBee - avatar
+ 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
8th Jul 2019, 5:07 AM
Mind To Machine 💻🕆
Mind To Machine 💻🕆 - avatar
0
Yes
19th Jul 2019, 4:31 PM
PAAVAN SRI SAI
PAAVAN SRI SAI - avatar