Premium Only Content

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.
-
43:16
Mike Rowe
4 days agoHe Manages Billions WITHOUT A College Degree?! | David Bahnsen #431 | The Way I Heard It
42.2K14 -
15:51
DeVory Darkins
1 day ago $14.14 earnedDemocrats suffer HUGE BLOW in court as contempt order paused
39.5K109 -
15:53
The Connect: With Johnny Mitchell
13 hours ago $6.86 earnedThe Truth About Ross Ulbricht & The Silk Road: How One Man Became The Internet's BIGGEST Drug Dealer
39.2K9 -
22:53
The Brett Cooper Show
2 days ago $9.34 earnedThe Rise of Fake Gurus & Online Cults | Episode 23
45.4K29 -
14:59:18
Total Horse Channel
17 hours agoWinnemucca Spring Spectacular Cow Horse Show and Derby - Saturday
46.2K5 -
LIVE
After Work Gaming
6 hours ago $1.34 earned“🍺 SOJU SATURDAY “🍺 THE FINALS SEASON 6
142 watching -
19:01
Russell Brand
13 hours agoTheo Von Just SHOCKED EVERYBODY With His Take on Trump Tariffs
94K107 -
14:22
Tundra Tactical
7 hours ago $5.07 earnedTRUMP DOJ Decides CZ Scorpion Is A Decepticon.
43.3K14 -
4:53:37
Culturama Podcast
5 hours ago $3.62 earnedEaster Special! Hollow Knight Stream!
33.3K4 -
1:04:29
Sarah Westall
8 hours agoThe Philippines and Massive Stores of Gold for all the People of the World w/ Joseph Allain
57.1K43