Cheat Sheets are in BETA. Give us your feedback.

While Loops in JavaScript

The JavaScript while loop creates a loop that is executed as long as a specified condition evaluates to true. The 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 should stop.

while (condition) { code block to be executed } // OR // i = 0 is the initialization let i = 0; // i < 5 is the condition while (i < 5) { console.log(i); // i++ is the incrementation i++; }

JavaScript Reverse Loop

A JavaScript for loop can iterate “in reverse” by initializing the loop variable to the starting value, testing for when the variable hits the ending value, and decrementing (subtracting from) the loop variable at each iteration.

const items = ['apricot', 'banana', 'cherry']; for (let i = items.length - 1; i >= 0; i -= 1) { console.log(`${i}: ${items[i]}`); } /* Console output: 2: cherry 1: banana 0: apricot */

do…while statement

A do...while statement creates a loop that executes a block of code once, checks if a condition is 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 condition evaluates to false.

// The syntax of a `do...while` statement. The value of `x` increases from 0 by intervals of `i` while `i` is less than 5: x = 0 i = 0 do { x = x + i; console.log(x) i++; } while (i < 5); // Output: 0 1 3 6 10

JavaScript For Loop

Loops execute a block of code multiple times. In JavaScript, a for loop declares looping instructions, with three important pieces of information separated by semicolons ;

  1. The initialization defines where to begin the loop by declaring (or referencing) the iterator variable
  2. The stopping condition determines when to stop looping (when the expression evaluates to false)
  3. The iteration statement updates the iterator each time the loop is completed
// A `for` loop that will loop through the numbers 0 to 3: for (let i = 0; i < 4; i += 1) { console.log(i); }; // `let i = 0` is the initialization // `i < 4` is the stopping condition // `i += 1` is the iteration statement // Output: 0, 1, 2, 3

.length property

A JavaScript array’s length can be evaluated with the .length property. array.length will return the exact length of the array in question. This is extremely helpful for looping through arrays, as the .length of the array can be used as the stopping condition in the loop.

// The syntax for looping through an array in JavaScript should look like this: for (let i = 0; i < array.length; i++){ console.log(array[i]); } // Output: every item in the array

JavaScript Break Keyword

Within a loop in JavaScript, the break keyword may be used to exit the loop immediately, continuing execution after the loop body.

// Here, the `break` keyword is used to exit // the loop when `i` is greater than 5: for (let i = 0; i < 99; i += 1) { if (i > 5) { break; } console.log(i) } // Output: 0 1 2 3 4 5

JavaScript Nested For Loop

In JavaScript, a nested for loop is when a for loop runs inside another for loop.

The inner loop will run all its iterations for each iteration of the outer loop.

for (let outer = 0; outer < 2; outer += 1) { 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 */

Stopping for(…) loops

The second expression in a JavaScript for loop is known as the stopping condition. It is used to determine whether to execute the statements in the code block or whether to exit the loop.

If the expression evaluates to true, the statements in the code block are executed. If the expression evaluates to false, the stopping condition has been met and the loop is exited.

// console outputs 0 1 2 for(let n=0; n<=2; n++){ console.log(n); }