A JavaScript for loop repeats a block of code a specific number of times when the number of iterations is known, utilizing three expressions: initialization, condition, and increment (or afterthought). The standard syntax is for (initialization; condition; afterthought) { // code block }, where the initialization runs once, the condition is checked before each iteration, and the afterthought executes after every loop pass.

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

Other iteration methods in JavaScript include:

  • while: Executes code as long as a specified condition is true.

  • do...while: Similar to while but guarantees the code block runs at least once before checking the condition.

  • for...in: Iterates over the enumerable properties of an object.

  • for...of: Iterates over the values of iterable objects like arrays, strings, Maps, and Sets.

  • forEach(): A method that iterates over each element in an array using a callback function.

🌐
W3Schools
w3schools.com › js › js_loop_for.asp
JavaScript for Loop
For Loops can execute a block of code a number of times.
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Loops_and_iteration
Loops and iteration - JavaScript | MDN
A for loop repeats until a specified condition evaluates to false. The JavaScript for loop is similar to the Java and C for loop.
Discussions

If I'm Struggling with JS Loops, Should I Even Bother?
Coding is hard, that's why not a lot of people do it. So I can assure you that this, you struggling, is going to happen again. You have to be ok with not fully understanding everything, mainly because you don't need to, to be a successful programmer. More on reddit.com
🌐 r/learnprogramming
49
21
May 9, 2023
[AskJS] Difference between For Loops
Some differences include: for loops for (let i = 0; i < arr.length; i++) { // loop code } User defined loop conditions Allows breaking (and continuing) Supports await/yield for surrounding scope Standard usage for arrays gives you index only, not value Tends to be more complicated than other loops (less readable, more error prone) But also most flexible in how looping is defined for of loops for (const element of arr) { // loop code } Loops through iterables only Allows breaking (and continuing) Supports await/yield for surrounding scope Standard usage for arrays gives you value only, not index Other helpers needed for specific kinds of iteration, like array.entries() to get indices and values, or Object.keys() for going through keys of objects For arrays, loops through all values, even holes when arrays are sparse for in loops for (const key in obj) { // loop code } Loops through any value Iterates through value keys, both own and inherited (Object.keys(), by comparison, only provides own keys, not inherited) Allows breaking (and continuing) Supports await/yield for surrounding scope For arrays does not include holes when arrays are sparse (not recommended for looping through arrays) forEach loops arr.forEach(function (element, index, arr) { // loop code }) Only usable for collections that have this method (arrays, typed arrays, and some others like NodeLists from DOM API) Uses a callback function rather than looping through a code block Does not allow breaking Does not support await/yield for surrounding scope (though you can await and yield in the callback itself, it doesn't affect the looping) Gives you value, index, and collection being looped for each iteration For arrays does not include holes when arrays are sparse Often considered the most readable, especially when reusing callback functions (arr.forEach(doThing)) though can also be least performant More on reddit.com
🌐 r/javascript
61
97
November 26, 2021
Are For Loops outdated?
You can use the "break" keyword in for, you can't do this in foreach. So for and foreach exist for different use case. More on reddit.com
🌐 r/learnjavascript
28
2
August 10, 2023
Help understanding for/in loops??
You're trying to see key as a parameter. But it's not a parameter, it's a variable declaration, much like i in your first for loop. You don't have to give it an initial value or increment it or anything though, the runtime does that for you as it iterates over object's properties; all you have to do is consume key. More on reddit.com
🌐 r/javascript
8
0
July 7, 2017
🌐
W3Schools
w3schools.com › js › js_loop_forin.asp
JavaScript For In
The for...in loop is primarily used for objects to access their property names (keys). for (key in object) { // code block to be executed } key A variable that holds the name (key) of each property during the iterations · object The object ...
🌐
W3Schools
w3schools.com › js › js_looping.asp
JavaScript Loops
JavaScript loops repeat executing a block of code a number of times. ... The for loop is used when the number of iterations is known.
🌐
CSS-Tricks
css-tricks.com › all-about-javascript-loops
All About JavaScript Loops | CSS-Tricks
August 21, 2024 - Every programming language has loops. Loops perform an operation (i.e., a chunk of work) a number of times, usually once for every item in an array or list, or to simply repeat an operation until a certain condition is met. JavaScript in particular has quite a few different types of loops.
🌐
Medium
learningdaily.dev › javascript-loops-tutorial-for-loop-while-loop-and-more-636f9f01a86a
JavaScript Loops Tutorial: for loop, while loop, and more | by The Educative Team | Dev Learning Daily
December 12, 2022 - Like most other programming languages, JavaScript provides loops that help us execute a statement or block of code repeatedly. A loop usually has the following properties: ... The loop starts with a LoopToken token, followed by a condition in parentheses, and concludes with a set of instructions encapsulated within the brackets. In the program we wrote earlier, the for statement is a LoopToken.
🌐
BitDegree
bitdegree.org › learn › javascript-for-loop
JavaScript for Loop: Ultimate For Loop JavaScript Tutorial
August 8, 2017 - It goes through the specified code an indicated amount of times according to the condition you set for it. The loop increments the specified variable each time as described in the original statement. for in JavaScript syntax example:
Find elsewhere
🌐
Career Karma
careerkarma.com › blog › javascript › javascript for loop: a step-by-step guide
JavaScript For Loop: A Step-By-Step Guide | Career Karma
December 1, 2023 - A JavaScript for loop executes a block of code as long as a specified condition is true. JavaScript for loops take three arguments: initialization, condition, and increment. The condition expression is evaluated on every loop.
🌐
Jstopics
jstopics.com › javascript › javascript-for-loops
JavaScript *for* Loops | JSTopics
In the second for loop we assign i to the length of the array and than just subtract from that number. The point is, condition evaluates those actual numbers. And in JavaScript, all of the numbers besides 0, return as a true boolean value. So when the i will equal to 0 loop will end.
🌐
DigitalOcean
digitalocean.com › community › tutorials › for-loops-for-of-loops-and-for-in-loops-in-javascript
JavaScript For Loops | DigitalOcean
August 26, 2021 - In this tutorial, we will learn ... of the JavaScript programming language. The for statement is a type of loop that will use up to three optional expressions to implement the repeated execution of a code block....
🌐
Udacity
udacity.com › blog › 2021 › 01 › javascript-for-loop.html
The Many Types of Javascript For Loop | Udacity
September 27, 2022 - The simplest type of for loop increments a variable as its iteration method. The variable acts as a counter for every “n”th element within an object. In this simple Javascript for loop example, the variable “i” increases once every time the loop is run.
🌐
freeCodeCamp
freecodecamp.org › news › javascript-for-loops
JavaScript For Loop – Explained with Examples
November 7, 2024 - We're only going to look at JavaScript ... The for loop is an iterative statement which you use to check for certain conditions and then repeatedly execute a block of code as long as those conditions are met....
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript tutorial › javascript for loop
JavaScript for Loop By Examples
November 15, 2024 - The for loop statement creates a loop with three optional expressions. The following illustrates the syntax of the for loop statement: for (initializer; condition; iterator) { // statements }Code language: JavaScript (javascript)
🌐
Medium
medium.com › codex › javascript-for-loop-techniques-you-might-not-know-cd543bc9931c
JavaScript “For” Loop Techniques You Might Not Know | by Jason Knight | CodeX | Medium
January 18, 2022 - There are a lot of techniques that can be leveraged to make them many times more effective than things like Array.foreach, Array.map, and so forth. Much less those cases where you just know you need to do “x” number of things. Some of the bizzaro-land hoops I’ve seen folks jump through just to avoid using a “for loop” is getting outright absurd.
🌐
Node.js
nodejs.org › en › learn › asynchronous-work › event-loop-timers-and-nexttick
Node.js — The Node.js Event Loop
There is a slight discrepancy between the Windows and the Unix/Linux implementation, but that's not important for this demonstration. The most important parts are here. There are actually seven or eight steps, but the ones we care about — ones that Node.js actually uses - are those above. pending callbacks: executes I/O callbacks deferred to the next loop iteration.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Statements › for...of
for...of - JavaScript | MDN
The for...of statement executes a loop that operates on a sequence of values sourced from an iterable object. Iterable objects include instances of built-ins such as Array, String, TypedArray, Map, Set, NodeList (and other DOM collections), as well as the arguments object, generators produced ...
🌐
Medium
medium.com › @techwithtwin › mastering-the-for-loop-in-javascript-a-beginners-guide-techwithtwin-6e12d831fd76
Mastering the for Loop in JavaScript: A Beginner’s Guide | TechWithTwin | by TechWithTwin | Medium
July 8, 2025 - The for loop is a powerful tool in JavaScript that loops over a range of numbers from the start to the point at which the conditions become true.
🌐
SheCodes
shecodes.io › athena › 5599-what-is-a-for-loop-in-javascript-and-how-to-use-it
[JavaScript] - What is a for loop in JavaScript and how to | SheCodes
Learn to use for loop in JavaScript with this tutorial that explains in detail how to iterate over a set of statements.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Statements › for
for - JavaScript | MDN
The for statement creates a loop that consists of three optional expressions, enclosed in parentheses and separated by semicolons, followed by a statement (usually a block statement) to be executed in the loop.
🌐
Codingem
codingem.com › home › javascript for loops — a step-by-step guide (examples)
JavaScript for Loops — A Step-by-Step Guide (Examples)
July 10, 2025 - In JavaScript, you can use a for loop to iterate over a group of values. For instance, you can go through an array of numbers.