Course

'for' and 'while' Loops

Loading...

In this lesson, you'll learn about and loops in JavaScript, which are essential for repeating actions multiple times efficiently.

For and While Loops

Sometimes we want to repeat an action a number of times. For example, let’s imagine we want to display numbers from zero to nine on the console. You might think of doing something like this:

console.log(0);
console.log(1);
console.log(2);
console.log(3);
console.log(4);
console.log(5);
console.log(6);
console.log(7);
console.log(8);
console.log(9);

But that’s not a good idea at all. Instead, we use or loops.

Loop

The loop is more complex, but it’s also the most commonly used loop. It’s called a loop because it runs "for" a specific number of times .

loops are declared with three optional expressions separated by semicolons: initialization, condition, and final-expression, followed by a statement (usually a block statement).

for ([initialization]; [condition]; [final-expression]) {
    // statement
}
  • Initialization: Executed once before the loop starts. It's typically used to define and set up your loop variable.
  • Condition: Evaluated at the beginning of every loop iteration and will continue as long as it evaluates to . If the condition is at the start of the iteration, the loop will stop executing.
  • Final-expression: Executed at the end of each loop iteration, prior to the next condition check, and is usually used to increment or decrement your loop counter.

Let's use a loop to print numbers from zero to nine:

for (let i = 0; i < 10; i++) {
    console.log(i);
}
  • Initialization: We initialize our variable because we start counting from 0. "i" stands for index and is a standard for loop variables.
  • Condition: We set our condition to . Before each loop execution, it checks if is less than ten. If is equal to or greater than 10, the loop terminates.
  • Final-expression: We use , which is shorthand for . Each iteration increases by one.

Optional Expressions in Loop

Expressions in a loop are optional, meaning we can skip parts. For example, we can initialize the loop variable before the loop like this:

let i = 0; // we have i already declared and assigned
for (; i < 10; i++) { // no need for "initialization"
    console.log(i); // 0, 1, 2, ..., 9
}

We can even remove everything, creating an infinite loop :

for (;;) {
    // repeats without limits
}

Loop

The loop is simpler than the loop and is used when the number of iterations is not known beforehand. It continues to execute a block of code as long as a specified condition evaluates to .

while (condition) {
    // statement
}
  • Condition: Evaluated before each loop iteration. If it evaluates to , the loop continues; if , the loop stops.

Let's use a loop to print numbers from zero to nine:

let i = 0;
while (i < 10) {
    console.log(i);
    i++;
}
  • Initialization: We initialize before the loop starts.
  • Condition: The loop continues as long as .
  • Increment: We manually increment within the loop using .

Infinite Loop

A loop can also become an infinite loop if the condition never evaluates to . Be cautious to ensure that the loop has a way to terminate:

while (true) {
    // This will run forever unless there's a break statement
}
Loading...

0 Comments

"Please login to view comments"

glass-bbok

Join the Conversation!

Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.

Upgrade your account
tick-guideNext Lesson

Functions Intro