menu

JavaScript/Typescript - 16 Topics

DEEP DIVE INTO

JavaScript/Typescript

Topic:for loop

menu

A for loop in JavaScript is a control structure that allows you to repeatedly execute a block of code a specified number of times or iterate over the elements of an iterable (like an array). It's one of the most commonly used looping mechanisms in programming.

Here's a deep explanation of the for loop:

Basic Syntax:

The basic syntax of a for loop consists of three parts enclosed in parentheses:

javascriptfor (initialization; condition; iteration) {
  // Code to be executed in each iteration
}
  • initialization: This is where you initialize a loop control variable. It's executed only once, at the beginning of the loop.

  • condition: The loop continues executing as long as the condition is true. If the condition is false at the start, the loop won't execute at all.

  • iteration: This part is executed at the end of each loop iteration. It typically increments or decrements the loop control variable.

Example:

Here's a simple example of a for loop that counts from 1 to 5:

javascriptfor (let i = 1; i <= 5; i++) {
  console.log(i);
}

In this example:

  • let i = 1 initializes the loop control variable i to 1.

  • i <= 5 is the condition, which is checked before each iteration. As long as it's true, the loop continues.

  • i++ is the iteration part, which increments i by 1 after each iteration.

Nested for Loops:

You can nest for loops inside one another to perform more complex iterations, like iterating over elements in a two-dimensional array.

Example:

Here's a nested for loop that generates a multiplication table:

javascriptfor (let i = 1; i <= 5; i++) {
  for (let j = 1; j <= 5; j++) {
    console.log(i * j);
  }
}

Loop Control Statements:

Inside a for loop, you can use loop control statements like break and continue to modify the loop's behavior.

  • break: Terminates the loop prematurely, even if the condition is still `true.

  • continue: Skips the current iteration and moves to the next one.

Example:

Here's a for loop that uses break and continue:

javascriptfor (let i = 1; i <= 5; i++) {
  if (i === 3) {
    break; // exit the loop when i equals 3
  }
  if (i === 2) {
    continue; // skip the iteration when i equals 2
  }
  console.log(i);
}

In this example, the loop breaks when i is 3 and skips the iteration when i is 2.

Infinite Loops:

Be careful when using for loops to avoid creating infinite loops, where the loop condition is always true, and the loop runs indefinitely. These can crash your application or web page.

danger

Example:

javascriptfor (let i = 1; i <= 5; i--) {
  console.log(i);
}

In this example, the loop control variable i is decreasing with each iteration, but the condition is i <= 5. Since i will always be greater than 5, this loop is infinite.

In summary, the for loop in JavaScript is a fundamental looping construct for executing a block of code a specific number of times or for iterating over elements in an iterable. It's versatile and powerful, allowing you to perform various tasks in your code, from simple counting to complex data manipulation.

1280 x 720 px