Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • JavaScript - Get Started
  • What is JavaScript
  • Setup JavaScript Dev Environment
  • HTML <script> Tag
  • JavaScript - Syntax
  • JavaScript - Popup Message
  • JavaScript - Variables
  • JavaScript - Operators
  • JavaScript - Data Types
  • JavaScript - String
  • JavaScript - Numbers
  • JavaScript - Boolean
  • JavaScript - Object
  • JavaScript - Date
  • JavaScript - Date Methods
  • JavaScript - Array
  • JavaScript - Array Methods
  • JavaScript - null and undefined
  • JavaScript - Function
  • JavaScript - if condition
  • JavaScript - switch
  • JavaScript - for loop
  • JavaScript - while loop
  • JavaScript - Scope
  • JavaScript - eval
  • JavaScript - Error Handling
  • JavaScript - strict mode
  • JavaScript - Hoisting
  • Define JS Class
  • JS Object In Depth
  • this Keyword
  • new Keyword
  • Prototype
  • Inheritance
  • Closure
  • IIFE
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

JavaScript for Loop

JavaScript includes for loop like Java or C#. Use for loop to execute code repeatedly.

Syntax:
for(initializer; condition; iteration)

The for loop requires following three parts.

  • Initializer: Initialize a counter variable to start with
  • Condition: specify a condition that must evaluate to true for next iteration
  • Iteration: increase or decrease counter
Example: for loop
for (var i = 0; i &lt; 5; i++)
{
    console.log(i);
}
Try it
Output:
0 1 2 3 4

In the above example, var i = 0 is an initializer statement where we declare a variable i with value 0. The second part, i < 5 is a condition where it checks whether i is less than 5 or not. The third part, i++ is iteration statement where we use ++ operator to increase the value of i to 1. All these three parts are separated by semicolon ;.

The for loop can also be used to get the values for an array.

Example: for loop
var arr = [10, 11, 12, 13, 14];

for (var i = 0; i &lt; 5; i++)
{
    console.log(arr[i]);
}
Try it
Output:
10 11 12 13 14

Please note that it is not mandatory to specify an initializer, condition and increment expression into bracket. You can specify initializer before starting for loop. The condition and increment statements can be included inside the block.

Example: for loop
var arr = [10, 11, 12, 13, 14];
var i = 0;

for (; ;) {
    
    if (i &gt;= 5)
    break;

    console.log(arr[i]);
        
    i++;
}
Try it
Output:
10 11 12 13 14

Learn about while loop in the next section.

Points to Remember :
  1. JavaScript for loop is used to execute code repeatedly.
  2. for loop includes three parts: initialization, condition and iteration. e.g.for(initializer; condition; iteration){ ... }
  3. The code block can be wrapped with { } brackets.
  4. An initializer can be specified before starting for loop. The condition and increment statements can be included inside the block.
TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.