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.
-
1:30:12
Kim Iversen
10 hours agoSecret Service Chief LIMITS Trump's Detail, GETS PROMOTED | “FAUCI LIED, BEAGLES DIED” Fauci Caught LYING To Congress
70.2K127 -
24:16
Stephen Gardner
7 hours ago🔴BREAKING: Bongino REVEALS new Trump Shooting Details + Kamala having issues
61.2K229 -
1:23:14
Michael Franzese
13 hours agoThis Was Blasphemy! Let’s Talk About the Olympics | MF LIVE | EP 14
61.1K46 -
2:06:00
Robert Gouveia
11 hours agoSecret Service REVOLTS in Leaked Email; Senators RIP Acting Director
75.9K135 -
2:25:52
Laura Loomer
8 hours agoEP63: Kamala Harris Caught Buying Social Media Influencers
72.4K65 -
53:33
Candace Owens
9 hours agoMore Occult History Exposed | Candace Ep 35
85.6K159 -
1:40:44
Glenn Greenwald
10 hours agoWhite "Dudes" and Women Rally for Harris; "Weird" J.D. Vance Attacks; Interviews with Political Analyst Bill Scher & Radio Host John Ziegler | SYSTEM UPDATE #307
108K183 -
2:26:25
WeAreChange
10 hours agoRIOTS ERUPT After 3 Girls Stabbed To Death In UK! CIVIL WAR??
90.1K85 -
1:14:44
Dr. Drew
15 hours agoPeter Navarro: Fresh Out Of Prison, Ex Trump Admin Official Fights Jan 6 "Lawfare" – Ask Dr. Drew
81.5K24 -
2:06:24
Tucker Carlson
12 hours agoSen. Mike Lee: Witnessing Biden’s Decline, and Obama Seizing Power From the Shadows
257K636