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 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
}
For example:
let i = 0;while (i < 5) {console.log(i);i++;}
The output would be:
01234
Do…While Loop
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 remains true. They are used when the loop body needs to be executed at least once. The loop ends when the condition evaluates to false.
let x = 0;let i = 0;do {x = x + i;console.log(x);i++;} while (i < 5);
The output would be:
013610
For Loop
A for
loop declares looping instructions, with three important pieces of information separated by semicolons ;
:
- The initialization defines where to begin the loop by declaring (or referencing) the iterator variable.
- 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.
for (let i = 0; i < 4; i += 1) {console.log(i);}
The output would be:
0123
for...of
Loop
A for...of
loop iterates over an object’s values rather than their keys. This allows for direct access to the items, as opposed to index-reference. Examples of iterable objects include:
const items = ['apple', 'banana', 'cherry'];for (const item of items) {console.log(item);}
The output would be:
applebananacherry
for...in
Loop
A for..in..
loop iterates over any object with string type keys and allows for access to the values by index-reference. The following accesses the keys:
const shoppingCart = { banana: 2, apple: 5, cherry: 0 };for (const fruit in shoppingCart) {console.log(fruit);}
The output would be:
bananaapplecherry
To access the values:
const shoppingCart = { banana: 2, apple: 5, cherry: 0 };for (const fruit in shoppingCart) {console.log(shoppingCart[fruit]);}
The output would be:
250
Reverse Loop
A 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]}`);}
The output would be:
2. cherry1. banana0. apricot
Looping Through Arrays
An array’s length can be evaluated with the .length
property. This is extremely helpful for looping through arrays, as the .length
of the array can be used as the stopping condition in the loop.
const fish = ['salmon', 'clown', 'whiting'];for (let i = 0; i < fish.length; i++) {console.log(fish[i]);}
The output would be:
salmonclownwhiting
Looping Through Objects
The Object.entries()
method can be used to loop through an object’s key-value pairs as an array. It returns an array of arrays, each of which represents a key-value pair. In the example below, Object.entries()
is used to define a variable called myEntries
:
const objectK = {name: 'Codecademy',age: 10,};const myEntries = Object.entries(objectK);console.log(myEntries);
The output would be:
[ [ 'name', 'Codecademy' ], [ 'age', 10 ] ]
Next, a for..of
loop can be used to print the key
‘s and value
‘s for myEntries
:
for (const [key, value] of myEntries) {console.log(`${key}: ${value}`);}
The output would be:
name: Codecademyage: 10
Break Keyword
Within a loop, 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);}
The output would be:
012345
Nested For Loop
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}`);}}
The output would be:
0-00-10-21-01-11-2
All contributors
- jochenRui48359635453 total contributions
- christian.dinh2476 total contributions
- valebases2 total contributions
- ajax81795339272 total contributions
- Anonymous contributorAnonymous contributor3071 total contributions
- jochenRui4835963545
- christian.dinh
- valebases
- ajax8179533927
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.