JavaScript is a very forgiving language. It’s easy to write code that runs but has mistakes in it.
In this article, we’ll look at the best practices for writing loops.
When to Use a for Loop?
We can use the for
loop for simple activities that don’t require loop controls inside the body.
So we can just let it run until the ending condition is met.
For instance, we can write:
for (let i = 0; i < 10; i++) {
//..
}
Then we end the loop when i
is 10.
If we don’t have a clear ending condition for the loop from the start, then we should use a while
loop.
When to Use a for-of Loop?
The for-of loop is great for looping through any arrays or other iterable objects.
It’s great for arrays, sets, maps, Nodelists, and other objects with the Symbol.iterator
method.
For instance, we can write:
const set = new Set([1, 2, 3]);
for (const s of set) {
console.log(s);
}
to loop through a set.
When to Use a for-in Loop?
We should never use the for-in loop. It loops through object keys in an indeterminate order and also iterates through inherited properties that are enumerable.
Therefore, we should just avoid it and use the Object.keys
method to get object keys as an array and loop through them.
Enter the Loop From One Location Only
We should only enter a loop from one location for consistency. Otherwise, confusion will result.
Put Initialization Code Directly Before the Loop
Loop initialization code should be put before the loop so that we make sure that we have all the data we need before the loop is run.
This way, we won’t forget to initialize the variables we need to run the loop later.
Use while( true ) for Infinite Loops
Sometimes we need to run infinite loops. We can run them with while(true)
and then terminate the loop later inside the loop body.
We don’t want a loop that may have a condition becoming false
sometimes.
for(;;)
may also be an acceptable alternative.
Prefer for Loops When They’re Appropriate
for
loops put all the loop control code in one place. This makes the code more readable.
It’s hard to forget to change the conditions if needed if they’re all in the same place rather than being scattered at different places.
Don’t Use a for Loop When a while Loop is More Appropriate
A while
loop may be more appropriate if we need to control a loop more flexibly than a for
loop can.
We don’t want to have for loops that have things that aren’t like:
for (let i = 0; i < 10; i++) {
//..
}
Use { and } to Enclose the Statements in a Loop
If our loop only has one statement, then we may skip the curly braces for looping the wrap the loop body.
However, we should do it for clarity. For instance, instead of writing the following:
if (condition)
doSomething();
We should write:
if (condition) {
doSomething();
}
This way, we won’t have issues with confusing where the loop body starts and ends.
Avoid Empty Loops
Empty loops are useless. Therefore, there’s no point in creating them.
We should write code for something more useful.
Keep Loop Housekeeping Chores at Either the Beginning or the End of the Loop
We can add housekeeping chores at the beginning of the end of the loop.
For instance, we can write something like:
while (i < 10) {
//...
i++;
}
so that have the i++
at the end of the loop body.
This way, we know that all the changes are in one place.
Make Each Loop Perform Only One Function
Loops shouldn’t do multiple things. It just makes our code hard to read for them to do multiple things.
Therefore, we should have each loop do their own functionality.
Assure Ourselves that the Loop Ends
If our loop isn’t an infinite loop, then we should make sure that it ends.
We got to think about the normal cases, the endpoints, and exceptional cases.
Make sure the loop ends. Otherwise, it’ll probably crash our program.
Conclusion
We shouldn’t have any deceiving code in our loops.
Therefore, we should always add curly braces around the loop body.
Also, for
loops are better in many cases since the conditions for starting and ending the loop are all at the top of the loop.
We should make sure that our loop ends in most cases.