Loops in JavaScript | Day 36 | For loop And While Loop | Web development course 2023
View other drafts
1
Loops in JavaScript
Loops are control flow statements that allow you to execute a block of code repeatedly until a specific condition is met. They are essential for automating tasks and processing large amounts of data. There are three primary loop types in JavaScript:
1. For loop:
The for loop iterates a block of code a specified number of times. It uses the following syntax:
JavaScript
for (initial_expression; condition; update_expression) {
// code to be executed
}
Use code with caution. Learn more
initial_expression: Initializes a counter variable.
condition: Determines whether the loop continues.
update_expression: Updates the counter variable after each iteration.
2. While loop:
The while loop iterates a block of code as long as a condition is true. It uses the following syntax:
JavaScript
while (condition) {
// code to be executed
}
Use code with caution. Learn more
condition: Determines whether the loop continues.
3. Do...while loop:
The do...while loop is similar to the while loop, but it executes the code block at least once before checking the condition. It uses the following syntax:
JavaScript
do {
// code to be executed
} while (condition);
Use code with caution. Learn more
condition: Determines whether the loop continues.
Here are some examples of each loop type:
1. For loop:
JavaScript
for (let i = 0; i < 5; i++) {
console.log(i); // Outputs: 0, 1, 2, 3, 4
}
Use code with caution. Learn more
2. While loop:
JavaScript
let count = 0;
while (count < 5) {
console.log(count); // Outputs: 0, 1, 2, 3, 4
count++;
}
Use code with caution. Learn more
3. Do...while loop:
JavaScript
let input;
do {
input = prompt("Enter a number:");
} while (isNaN(input)); // Ensures valid input
console.log("You entered:", input);
Use code with caution. Learn more
In addition to these basic loop types, JavaScript offers several other loop variations and control flow statements for more complex situations. These include:
for...in loop: Iterates over the properties of an object.
for...of loop: Iterates over the values of an iterable object.
break statement: Exits the current loop.
continue statement: Skips the current iteration of the loop and continues to the next.
-
0/2000
-
14:38
Cowboy Kent Rollins
11 days ago $1.04 earnedMountain Man Breakfast | Hearty Breakfast Casserole
1.43K4 -
LIVE
Game On!
16 hours agoJalen Hurts DODGES Trump! Skips Team White House Visit!
11,573 watching -
LIVE
BEK TV
23 hours agoTrent Loos in the Morning 4/29/2025
5,919 watching -
5:52
MichaelMotamedi
11 hours ago $2.48 earnedEating The Most Elusive Lobster In The World
14K17 -
11:03
Melonie Mac
14 hours agoClair Obscure Expedition 33 Has Saved Gaming
24.2K33 -
7:39
The Shannon Joy Show
15 hours ago🇺🇸 America's Breaking Point: Are We Ready to Toss the Board? 🎯
18.9K18 -
10:00
Adam Does Movies
19 hours ago $2.86 earnedCEO Claims Netflix Saved Hollywood. Um, WHAT?!?! - Rant
31.5K8 -
1:00:43
Trumpet Daily
20 hours ago $5.25 earnedThat Big, Beautiful Wall Around the Vatican - Trumpet Daily | Apr. 28, 2025
17.1K22 -
12:40
Degenerate Jay
13 hours ago $0.62 earnedThe Perfect RoboCop Versus Terminator Game Needs To Be Made!
20.1K5 -
8:17
VSOGunChannel
16 hours ago $0.83 earnedYou Were Put on a Government Watch List || DECLASSIFIED
20.4K9
0 Comments