Skip to content

Latest commit

 

History

History
59 lines (54 loc) · 1.07 KB

File metadata and controls

59 lines (54 loc) · 1.07 KB

JavaScript For Loop

Description

The JavaScript for loop is a control flow statement that allows you to execute a block of code repeatedly. It consists of three optional expressions: initialization, condition, and iteration. The loop continues to execute as long as the condition is true. Here's the basic syntax:

for (initialization; condition; iteration) {
  // code to be executed
}

Features

Initialization: Declare and initialize a loop variable.
Condition: Specify the condition to continue the loop.
Iteration: Define how the loop variable changes after each iteration.

Example

Simple Numeric Loop

for (let i = 0; i < 5; i++) {
console.log(i);
}

This will output:

0
1
2
3
4

Looping Through an Array

const fruits = ['apple', 'orange', 'banana'];

for (let i = 0; i < fruits.length; i++) {
  console.log(fruits[i]);
}

This will output:

apple
orange
banana

Backward Loop

for (let i = 5; i > 0; i--) {
  console.log(i);
}

This will output:

5
4
3
2
1