🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Loops_and_iteration
Loops and iteration - JavaScript - MDN Web Docs - Mozilla
Otherwise, the for loop terminates. (If the condition expression is omitted entirely, the condition is assumed to be true.) The statement executes. To execute multiple statements, use a block statement ({ }) to group those statements. If present, the update expression afterthought is executed. Control returns to Step 2. In the example below, the function contains a for statement that counts the number of selected options in a scrolling list (a <select> element that allows multiple selections).
🌐
W3Schools
w3schools.com › js › js_loop_for.asp
JavaScript for Loop
When let is used to declare the i variable in a loop, the i variable will only be visible within the loop. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
🌐
Programiz
programiz.com › javascript › for-loop
JavaScript for loop (with Examples)
In this example, we used the for loop to print "Hello, world!" three times to the console. ... Once an iteration of the loop is completed, the condition is evaluated again. The process continues until the condition is false. To learn more about the condition, visit JavaScript Comparison and ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Statements › for
for - JavaScript | MDN - MDN Web Docs - Mozilla
These details can be observed by ... point. For example, in this code a closure created within the initialization section does not get updated by re-assignments of i in the afterthought: ... This does not log "0, 1, 2", like what would happen if getI is declared in the loop ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › loops-in-javascript
JavaScript Loops - GeeksforGeeks
It contains initialization, condition, and increment/decrement in one line. ... Example: The below JavaScript program for loop runs from i = 1 to i = 3, incrementing i by 1 each time, and prints "Count:" followed by the current value of i.
Published   January 19, 2026
🌐
freeCodeCamp
freecodecamp.org › news › javascript-for-loops
JavaScript For Loop – Explained with Examples
November 7, 2024 - The code block above is the standard syntax used by for loop. Let's look at each parameter to see what it means and what it does: initialExpression: This is used to set the value of a counter variable, and it is only evaluated once, before the loop starts. Depending on the scope, these counter variables are usually declared with the var or let keywords. condition: This is a constant-evaluation expression that determines whether the loop should be executed.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-for-loop
JavaScript For Loop - GeeksforGeeks
// for loop begins when x=2 // and runs till x <= 4 for (let x = 2; x <= 4; x++) { console.log("Value of x:" + x); } ... For loop to print table of a number. ... For loop to print elements of an array.
Published   September 27, 2025
🌐
TutorialsPoint
tutorialspoint.com › javascript › javascript_for_loop.htm
JavaScript - For Loop
Starting Loop Current Count : 0 Current Count : 1 Current Count : 2 Current Count : 3 Current Count : 4 Current Count : 5 Current Count : 6 Current Count : 7 Current Count : 8 Current Count : 9 Loop stopped!
Find elsewhere
🌐
Mimo
mimo.org › glossary › javascript › for-loops
JavaScript For Loop: Efficient Iteration in JavaScript
Here’s the example from above, using a for of loop instead of a for loop: ... The for of loop simplifies iterating through an arr, making code cleaner. JavaScript's array forEach() method is another form of loop that executes a function once per array element.
🌐
W3Schools
w3schools.com › js › js_loop_forin.asp
JavaScript For In
JS Examples JS HTML DOM JS HTML Input JS HTML Objects JS HTML Events JS Browser JS Editor JS Exercises JS Quiz JS Website JS Syllabus JS Study Plan JS Interview Prep JS Bootcamp JS Certificate JS Reference ... The for...in loop iterates over the enumerable properties of an object.
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript tutorial › javascript for loop
JavaScript for Loop By Examples
November 15, 2024 - JavaScript allows the for statement to have an empty statement. In this case, you place a semicolon (;) immediately after the for statement. For example, the following uses a for loop to calculate the sum of 10 numbers from 1 to 10:
🌐
W3Schools
w3schools.com › js › js_loops.asp
JavaScript Loops
In the following example, the code in the loop will run, over and over again, as long as a variable (i) is less than 10: while (i < 10) { text += "The number is " + i; i++; } Try it Yourself » · If you forget to increase the variable used in the condition, the loop will never end.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Statements › for...in
for...in - JavaScript | MDN - MDN Web Docs
The for...in statement iterates over all enumerable string properties of an object (ignoring properties keyed by symbols), including inherited enumerable properties.
🌐
Programiz
programiz.com › javascript › for-in
JavaScript for...in loop (with Examples)
The JavaScript for...in loop iterates over the keys of an object. Here's a simple example of the for...in loop in JavaScript.
Top answer
1 of 4
10

IF you have array than make use of forEach

array.forEach(ele=> {

});

that way you can keep code clean and easy to understand and dont have to do length related code.

Break is not going to work with forEach but you can write return for coming out of forEach like

array.forEach(ele=> {
   ele.array.forEach(ele=> {
     //do stuff 
     return;
   });
});

Note:

  1. for loop is faster.
  2. forEach is slower, but better fits functional programming paradigms.

Answer is based on title of question : Best Practice that why given suggestion to make use of forEach over for.

2 of 4
7

Concerning saving array.length: Even though in the early days of JavaScript it made a difference to save the .length value in a variable, modern JavaScript engines will run the for loop just as fast if you write it as in the first version.

Iterating backward is also not guaranteed to run faster on modern engines. There is also the consideration that CPUs are optimised to anticipate forward memory references, although this will only be relevant when the JS engine has decided to store the array as a contiguous block of memory.

As for the use of labels: most would not consider this best practice. Where the previous optimisation (concerning .length) concerns all iterations of the loop, this issue only applies to a single exit out of both loops. Whatever solution you use, it represents constant time, and could not be a determining factor in the overall performance of the loop.

So certainly in this case, I would go with good coding habits over tiny optimisation considerations. When you want to exit just the current loop, a single break is enough.

If you want to exit quickly from nested loops, then consider placing them in a function, so you can use return:

function performNestedLoop(array, array2) {
    for(let i = 0; i < array.length; i++){
        for(var j = 0; j < array2.length; j++){
            //dostuff
            if (exitCondition) return;
        }
    }
}

As to best practice: this really depends on what you need to achieve. There are several array methods that provide iteration, like forEach, map, reduce, reduceRight, filter, some, every, find, findIndex, includes,... each with their purpose. If they fit the purpose, then go with them. If you don't need the index, but just the value, then use for..of. If you really need every speck of optimatisation, then the old fashioned for loop is a likely winner.

🌐
TutorialsTeacher
tutorialsteacher.com › javascript › javascript-for-loop
for loop in JavaScript
var arr = [10, 11, 12, 13, 14]; var i = 0; for (; ;) { if (i &gt;= 5) break; console.log(arr[i]); i++; } ... Learn about while loop in the next section. ... JavaScript for loop is used to execute code repeatedly.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › for-in-loop-in-javascript
JavaScript For In Loop - GeeksforGeeks
The for...in loop in JavaScript is used to iterate over the enumerable properties of an object.
Published   January 22, 2026
🌐
Tutorial Republic
tutorialrepublic.com › javascript-tutorial › javascript-loops.php
JavaScript While, Do-While, For and For-In Loops - Tutorial Republic
The following example defines a loop that starts with i=1. The loop will continued until the value of variable i is less than or equal to 5. The variable i will increase by 1 each time the loop runs: ... The for loop is particularly useful for iterating over an array. The following example will show you how to print each item or element of the JavaScript array.