codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🌐

JavaScript

11 / 65 topics
9JavaScript if...else Statement10JavaScript switch...case Statement11JavaScript for Loop12JavaScript while and do...while Loop13JavaScript break and continue
Tutorials/JavaScript/JavaScript for Loop
🌐JavaScript

JavaScript for Loop

Updated 2026-05-12
15 min read

JavaScript for Loop

In this tutorial, you'll learn how to use the standard for loop structure in JavaScript. The for loop is a fundamental control flow statement that allows you to execute a block of code repeatedly based on a specific condition. Understanding how to use loops effectively is crucial for any programmer, as they enable you to automate repetitive tasks and process large amounts of data efficiently.

Introduction

The for loop is one of the most commonly used looping constructs in JavaScript. It provides a concise way to iterate over arrays, strings, or other collections of elements. By controlling the number of iterations through initialization, condition checking, and iteration updates, you can execute code repeatedly until a certain condition is met.

Core Content

Standard for Loop Structure

The standard for loop in JavaScript follows this syntax:

JavaScript
1for (initialization; condition; update) {
2// Code to be executed
3}
  • Initialization: This part of the loop is executed only once, before the loop starts. It's typically used to initialize a counter variable.
  • Condition: Before each iteration, this condition is evaluated. If it evaluates to true, the loop body executes; if false, the loop terminates.
  • Update: After each iteration of the loop body, this part is executed. It's usually used to update the counter variable.

Example: Basic for Loop

Let's start with a simple example where we print numbers from 1 to 5 using a for loop:

basicForLoop.js
1for (let i = 1; i <= 5; i++) {
2console.log(i);
3}
Output
1
2
3
4
5

In this example:

  • let i = 1 initializes the counter variable i to 1.
  • i <= 5 is the condition that checks if i is less than or equal to 5. As long as this condition is true, the loop continues.
  • i++ increments the value of i by 1 after each iteration.

Looping Through Arrays

One of the most common use cases for loops is iterating over arrays. The for loop can be used to access and manipulate each element in an array.

Example: Looping Through an Array

Let's create an array of fruits and print each fruit using a for loop:

loopThroughArray.js
1const fruits = ['apple', 'banana', 'cherry'];
2for (let i = 0; i < fruits.length; i++) {
3console.log(fruits[i]);
4}
Output
apple
banana
cherry

In this example:

  • const fruits = ['apple', 'banana', 'cherry']; initializes an array of strings.
  • let i = 0 starts the loop with the first index of the array.
  • i < fruits.length ensures that the loop continues until it has iterated over all elements in the array.
  • i++ increments the counter to move to the next element.

Looping Through Strings

Strings can also be treated as arrays of characters, allowing you to iterate over each character using a for loop.

Example: Looping Through a String

Let's print each character of a string using a for loop:

loopThroughString.js
1const str = "Hello";
2for (let i = 0; i < str.length; i++) {
3console.log(str[i]);
4}
Output
H
e
l
l
o

In this example:

  • const str = "Hello"; initializes a string.
  • The loop iterates over each character in the string, printing them one by one.

Common Mistakes

  1. Off-by-One Errors: One of the most common mistakes is incorrect initialization or update expressions, leading to off-by-one errors. For example, using i <= length instead of i < length can cause an "index out of bounds" error.
  2. Infinite Loops: If the condition never becomes false, the loop will run indefinitely, causing a potential crash. Ensure that the update expression correctly modifies the counter variable to eventually meet the condition.

Practical Example

Let's create a practical example where we use a for loop to calculate the sum of all numbers in an array and find the average:

practicalExample.js
1const numbers = [10, 20, 30, 40, 50];
2let sum = 0;
3
4for (let i = 0; i < numbers.length; i++) {
5sum += numbers[i];
6}
7
8const average = sum / numbers.length;
9console.log(`Sum: ${sum}`);
10console.log(`Average: ${average}`);
Output
Sum: 150
Average: 30

In this example:

  • We initialize an array of numbers.
  • We use a for loop to iterate over the array, summing up all the elements.
  • Finally, we calculate and print the average of the numbers.

Summary

ConceptDescription
InitializationExecuted once before the loop starts. Used to initialize variables.
ConditionChecked before each iteration. Loop continues as long as this condition is true.
UpdateExecuted after each iteration. Typically used to update counter variables.
  • The for loop is a powerful tool for iterating over arrays and strings.
  • Proper initialization, condition checking, and updating are crucial for effective looping.
  • Be cautious of off-by-one errors and infinite loops.

What's Next?

In the next tutorial, you'll learn about other types of loops in JavaScript, including while and do...while loops. These loops offer different ways to control the flow of your program based on specific conditions, allowing for more flexible and dynamic code execution. Keep exploring these essential concepts to enhance your programming skills!


PreviousJavaScript switch...case StatementNext JavaScript while and do...while Loop

Recommended Gear

JavaScript switch...case StatementJavaScript while and do...while Loop