loader

Loading

CoderrShyam logo

CoderrShyam

HomeAboutContactBlogTutorials
LoginSignup

Introduction

Quick StartWhat is JavaScriptHistory of JavaScript

Setup

Node.js InstallationVisual Studio Code

Basics

Syntax & Comments
Variables
For LoopsWhile Loop
Functions

Advanced

CoderrShyam logo

CoderrShyam

HomeAboutContactBlogTutorials
LoginSignup
TutorialsJavaScript TutorialLoopsFor Loops

For Loops

For loops are a common control flow structure in programming that allows you to repeat a block of code a specific number of times.

In JavaScript, there are three types of for loops:

  1. Standard For Loop
  2. For-In Loop
  3. For-Of Loop

Standard For Loop

The standard for loop is the most common type of loop in JavaScript. It allows you to execute a block of code a specific number of times. Here is the syntax for a standard for loop:

main.js
for (initialization; condition; update) {
  // code to be executed
}

where:

  • The initialization is executed before the loop starts. It is typically used to initialize a counter variable.
  • The condition is evaluated before each iteration of the loop. If the condition is true, the code inside the loop is executed; otherwise, the loop terminates.
  • The update is executed after each iteration of the loop. It is typically used to update the counter variable.

Here is an example of using a standard for loop in JavaScript:

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

In this example, the loop will execute 5 times, and the output will be:

0
1
2
3
4
5

For-In Loop

The for-in loop is used to iterate over the properties of an object. Here is the syntax for a for-in loop:

main.js
for (variable in object) {
  // code to be executed
}

where:

  • The variable is a string that represents the name of a property in the object.
  • The object is the object over which to iterate.

Here is an example of using a for-in loop in JavaScript:

main.js
let person = {
  name: "John",
  age: 30,
};

for (let key in person) {
  console.log(key + ": " + person[key]);
}

In this example, the loop will iterate over the properties of the person object and output:

name: John
age: 30

For-Of Loop

The for-of loop is used to iterate over the elements of an iterable object (e.g., an array). Here is the syntax for a for-of loop:

main.js
for (variable of iterable) {
  // code to be executed
}

where:

  • The variable is a reference to the current element in the iterable.
  • The iterable is the object over which to iterate.

Here is an example of using a for-of loop in JavaScript:

main.js
let colors = ["red", "green", "blue"];

for (let color of colors) {
  console.log(color);
}

In this example, the loop will iterate over the elements of the colors array and output:

red
green
blue

Conclusion

  • The standard for loop is used to execute a block of code a specific number of times.
  • The for-in loop is used to iterate over the properties of an object.
  • The for-of loop is used to iterate over the elements of an iterable object.

How is this guide?

February 26th, 2026

Ternary Operator

The "ternary operator" is a shorthand way of writing an "if-else" statement in JavaScript.

While Loop

The "while" loop in JavaScript is used to execute a block of code as long as a condition is true.

© 2026CoderrShyamAll Rights Reserved.

On this page

Standard For Loop
For-In Loop
For-Of Loop
Conclusion
Follow us on GitHub