0% found this document useful (0 votes)
2 views

7.Learn JavaScript_ Loops Cheatsheet _ Codecademy

The document provides an overview of various loop structures in JavaScript, including reverse loops, do...while statements, for loops, while loops, and nested for loops. It explains how to iterate through arrays, use the break keyword to exit loops, and the general concept of looping as a means to repeat instructions until a specified condition is met. Each type of loop is illustrated with code examples and their expected outputs.

Uploaded by

toranjazad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

7.Learn JavaScript_ Loops Cheatsheet _ Codecademy

The document provides an overview of various loop structures in JavaScript, including reverse loops, do...while statements, for loops, while loops, and nested for loops. It explains how to iterate through arrays, use the break keyword to exit loops, and the general concept of looping as a means to repeat instructions until a specified condition is met. Each type of loop is illustrated with code examples and their expected outputs.

Uploaded by

toranjazad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Cheatsheets / Learn JavaScript

Loops

Reverse Loop

A for loop can iterate “in reverse” by initializing the const items = ['apricot', 'banana',
loop variable to the starting value, testing for when the
'cherry'];
variable hits the ending value, and decrementing
(subtracting from) the loop variable at each iteration.
for (let i = items.length - 1; i >= 0; i
-= 1) {
console.log(`${i}. ${items[i]}`);
}

// Prints: 2. cherry
// Prints: 1. banana
// Prints: 0. apricot

Do…While Statement

A do...while statement creates a loop that x = 0


executes a block of code once, checks if a condition is
i = 0
true, and then repeats the loop as long as the condition
is true. They are used when you want the code to
always execute at least once. The loop ends when the do {
condition evaluates to false.
x = x + i;
console.log(x)
i++;
} while (i < 5);

// Prints: 0 1 3 6 10
For Loop

A for loop declares looping instructions, with three for (let i = 0; i < 4; i += 1) {
important pieces of information separated by
console.log(i);
semicolons ; :
The initialization defines where to begin the };
loop by declaring (or referencing) the iterator
variable
// Output: 0, 1, 2, 3
The stopping condition determines when to
stop looping (when the expression evaluates to
false )
The iteration statement updates the iterator
each time the loop is completed

Looping Through Arrays

An array’s length can be evaluated with the for (let i = 0; i < array.length; i++){
.length property. This is extremely helpful for
console.log(array[i]);
looping through arrays, as the .length of the array
can be used as the stopping condition in the loop. }

// Output: Every item in the array

Break Keyword

Within a loop, the break keyword may be used to for (let i = 0; i < 99; i += 1) {
exit the loop immediately, continuing execution after
if (i > 5) {
the loop body.
Here, the break keyword is used to exit the loop break;
when i is greater than 5. }
console.log(i)
}

// Output: 0 1 2 3 4 5
Nested For Loop

A nested for loop is when a for loop runs inside for (let outer = 0; outer < 2; outer +=
another for loop.
1) {
The inner loop will run all its iterations for each
iteration of the outer loop. for (let inner = 0; inner < 3; inner +=
1) {
console.log(`${outer}-${inner}`);
}
}

/*
Output:
0-0
0-1
0-2
1-0
1-1
1-2
*/

Loops

A loop is a programming tool that is used to repeat a set


of instructions. Iterate is a generic term that means “to
repeat” in the context of loops. A loop will continue to
iterate until a specified condition, commonly known as
a stopping condition, is met.
While Loop

The while loop creates a loop that is executed as while (condition) {


long as a specified condition evaluates to true . The
// code block to be executed
loop will continue to run until the condition evaluates
to false . The condition is specified before the loop, }
and usually, some variable is incremented or altered in
the while loop body to determine when the loop let i = 0;
should stop.

while (i < 5) {
console.log(i);
i++;
}

Print Share

You might also like