
do while With falsy
JavaScript, do while plays an important role because it’s different from other javascript loops. Because do-while at least run once then it checks whether he needs to run a do or not based on condition passed to while that can falsy tr truthy.
Basic Syntax of do-while
do{
}while(condition)
For more detail about the javascript while loop.
JavaScript Question
In this javascript quiz, find out that do act then while checking because do while runs once at least then it check for condition.
First we declare a variable num using let keyword and assign an number 0 to it.
let num = 0;
In the next step, we use do while loop in while as condition a num value is passed and inside a do block. We are printing a value of num.
do{
console.log(num)
} while(num);
Any idea what should be the output of above code statement.
JavaScript Quiz Test
Here is the full javascript code snippet for you to solve.
let num = 0;
do{
console.log(num)
} while(num);
// output => 🧐 ?
Answer – 0
Click To Run Code
Explanation
Now, you know the answer is 0. But why because 0 is falsy value. Let me explain.
As, we already know that do runs whether the while have falsy condtition or truthy condition.
That’s makes clear that output will print 0. But it only print once. Why ?
Because 0 is a falsy value. Which means do block will only runs after it.
At last, That’s why we get an answer “0“.
Let me know in the comment section, what you think about it.
I hope you understand the concept and logic behind it.
But wait, i have something more for you –
Check out our other javascript quiz –
- JavaScript Quiz – does false compare with false return false
- JS Quiz – can array have the size method like a set
- JS Quiz – access JSON string value is possible
- JavaScript Quiz – can divide assignment operator work in one go
Conclusion
To participate in our javascript quiz or challenges, tutorial, tips & tricks make sure to join our jsstartup newsletter. So, you can able to participate in our daily challenges & learn the javascript concept.
And last but not the least, don’t forget to like, comment and share. It gives us a morale boost to remain to continue. Also, join our Facebook Page
If you have any questions, please feel free to ask me in the comment section and also let me know if you have any suggestions. As suggestions are always welcome.