While Loops in JavaScript
Read This on Github
⭐ Syntax of For Loop
While Loops Syntax Breakdown
while (condition) {
// code to be executed
}
Condition
: This is the first part of the while loop and is used to specify the condition that must be met for the loop to continue running. As long as the condition is true, the loop will continue to execute. When the condition becomes false, the loop will stop. For example:
i < 10
Code block
: If the condition is true, the code inside the while loop's code block will be executed. This is the second part of the while loop. For example:
while (condition) {
console.log(i);
}
In this example, the code block consists of a single line that prints the value of i to the console.
Here is an example of how a while loop can be used in JavaScript:
let i = 0;
while (i < 10) {
console.log(i);
i++;
}
This while loop will print the numbers 0 through 9 to the console. The loop will start by evaluating the condition i < 10
, which will be true because the value of i
is 0. The
⭐ Explaining Step by Step of While Loop
While Loops Explained with Example
💥 While Loop Example
let count = 0;
while (count < 3) {
console.log(count);
count++;
}
💥 Visualize the Example
1. count = 0
2. if count < 3, enter loop
3. print count
4. count++
5. if count < 3, go back to step 3
6. if count >= 3, end loop
💥 Explain the Example
-
A variable
count
is defined and initialized to0
. -
The while loop begins with the condition
count < 3
.- At this point, the value of
count
is 0, so the conditioncount < 3
istrue
. - The loop body runs, and the current value of
count (0)
is logged to the console. - The value of
count
is incremented by1
(using the postfix increment operator++
). The value ofcount
is now1
. - The loop checks the condition again. The condition
count < 3
is stilltrue
, so the loop runs again.
- At this point, the value of
-
The loop body runs again, and the current value of count (1) is logged to the console.
- The value of
count
is incremented by 1 (using the postfix increment operator ++). The value ofcount
is now 2. - The loop checks the condition again. The condition
count < 3
is stilltrue
, so the loop runs again.
- The value of
-
The loop body runs again, and the current value of
count (2)
is logged to the console.- The value of
count
is incremented by 1 (using the postfix increment operator++
). The value ofcount
is now 3. - The loop checks the condition again. The condition
count < 3
is nowfalse
, so the loop ends.
- The value of
-
The loop has ended, and the program continues with any code that follows the loop.
⚡ Playground
⭐ Example of While Loop
While Loops Example for Beginners
✨ 1. Printing the numbers from 1 to 10:
let i = 1;
while (i <= 10) {
console.log(i);
i++;
}
✨ 2. Printing the numbers from 10 to 1:
let i = 10;
while (i >= 1) {
console.log(i);
i--;
}
✨ 3. Printing the even numbers from 1 to 10:
let i = 2;
while (i <= 10) {
console.log(i);
i += 2;
}
✨ 4. Printing the odd numbers from 1 to 10:
let i = 1;
while (i <= 10) {
console.log(i);
i += 2;
}
✨ 5. Calculating the sum of the first 10 numbers:
let sum = 0;
let i = 1;
while (i <= 10) {
sum += i;
i++;
}
console.log(sum);
✨ 6. Calculating the sum of the first 10 even numbers:
let sum = 0;
let i = 2;
while (i <= 10) {
sum += i;
i += 2;
}
console.log(sum);
✨ 7. Calculating the sum of the first 10 odd numbers:
let sum = 0;
let i = 1;
while (i <= 10) {
sum += i;
i += 2;
}
console.log(sum);
✨ 8. Printing the numbers from 1 to 10 in reverse:
let i = 10;
while (i >= 1) {
console.log(i);
i--;
}
📚 Extra
Infinite Loops - A loop that never ends is called an infinite loop. For example, the following loop is an infinite loop:
while (true) {
console.log("Hello World");
}
The condition true
is always true
, so the loop never ends. If you run this code, it will never stop printing "Hello World" to the console.