This loop will execute until the condition is given false.
Syntax:-
while(condition){
// logic implement
}
Example:- Now, you have to print the i value until the i value not more than 20.
var i=0;
while(i<=20){
console.log(i);
i++;
}
Output:-
0
1
.
.
0
1
.
.
19
20
Example:- Now, you have to print the even i value until the i value not more than 10.
var i=2;
while(i<=10){
if(i%2 ==0){
console.log(i);
}
i++;
}
Output:-
2
4
6
8
10
2
4
6
8
10
Javascript While loop – Interview Questions
Q 1: What is a while loop?
Ans: Executes while the condition is true.
Q 2: Is it entry-controlled?
Ans: Yes.
Q 3: When to use a while loop?
Ans: When iterations are unknown.
Q 4: Can a while loop run infinitely?
Ans: Yes, if the condition is always true.
Q 5: Is a break supported?
Ans: Yes.
Javascript While loop – Objective Questions (MCQs)
Q1. The while loop checks the condition ______.
Q2. The while loop is used when the number of iterations is ______.
Q3. If the condition is false initially, the loop will execute ______.
Q4. Which keyword skips the current iteration?
Q5. A missing condition update may cause ______.