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.
-
BlackDiamondGunsandGear
9 hours agoAFTER HOURS ARMORY w/ DLD & John from GOA & FLR
16.8K3 -
1:05:28
Man in America
10 hours agoTREASON? Obama, Hillary, and Soros in the New World Order Agenda EXPOSED w/ Mel K
68.9K52 -
2:22:46
The Connect: With Johnny Mitchell
11 hours ago $1.97 earnedOne Man's Mission To Stop Human Trafficking: How A Billionaire Mercenary Saved Hundreds Of Children
14.3K8 -
2:35:13
Tundra Tactical
6 hours ago $3.09 earned🔫 California Ammo Win, Sig Sauer P320 Controversy, Meme Review & Would You Rather! 🎉🔥
27.9K4 -
16:24
Forrest Galante
4 hours ago6 Deadliest Man Eaters to Ever Exist
19.6K3 -
10:14
MattMorseTV
9 hours ago $10.78 earnedThe EU is in HOT WATER.
83.8K45 -
LIVE
The Rabble Wrangler
1 day agoPUBG with The Best in the West!
413 watching -
3:57:19
EvilT4000
7 hours ago $1.58 earnedSaturday.....🟢For energy and focus click my Dubby link!
24.8K3 -
3:02:55
SlingerGames
3 hours agoSpartan Night - Halo and More | Creator for @SELFMADEGGS
16.4K -
3:57:49
Mally_Mouse
9 hours agoSpicy Saturday!! - 10k CELEBRATION! - Let's Play: Labyrinthine
32K1