We often need to repeat actions. Show For example, outputting goods from a list one after another or just running the same code for each number from 1 to 10. Loops are a way to repeat the same code multiple times. The for…of and for…in loops A small announcement for advanced readers. This article covers only basic loops: If you came to this article searching for other types of loops, here are the pointers:
Otherwise, please read on. The “while” loopThe
While the For instance, the loop below outputs
A single execution of the loop body is called an iteration. The loop in the example above makes three iterations. If Any expression or variable can be a loop condition, not just comparisons: the condition is evaluated and converted to a boolean by For instance, a shorter way to write
Curly braces are not required for a single-line body If the loop body has a single statement, we can omit the curly braces
The “do…while” loopThe condition check can be moved below the loop body using the
The loop will first execute the body, then check the condition, and, while it’s truthy, execute it again and again. For example:
This
form of syntax should only be used when you want the body of the loop to execute at least once regardless of the condition being truthy. Usually, the other form is preferred: The “for” loopThe It looks like this:
Let’s learn the meaning of these parts by example. The loop below runs
Let’s examine the
The general loop algorithm works like this:
That is, If you are new to loops, it could help to go back to the example and reproduce how it runs step-by-step on a piece of paper. Here’s exactly what happens in our case:
Inline variable declaration Here, the “counter” variable
Instead of defining a variable, we could use an existing one:
Skipping partsAny part of For example, we can omit Like here:
We can also remove the
This makes the loop identical to We can actually remove everything, creating an infinite loop:
Please note that the two Breaking the loopNormally, a loop exits when its condition becomes falsy. But we can force the exit at any time using the special For example, the loop below asks the user for a series of numbers, “breaking” when no number is entered:
The The combination “infinite loop + Continue to the next iterationThe We can use it if we’re done with the current iteration and would like to move on to the next one. The loop below uses
For even values of The A loop that shows odd values could look like this:
From a technical point of view, this is identical to the example above. Surely, we can just wrap the code in an But as a
side effect, this created one more level of nesting (the No Please note that syntax constructs that are not expressions cannot be used with the ternary operator For example, if we take this code:
…and rewrite it using a question mark:
…it stops working: there’s a syntax error. This is just another reason not to use the question mark operator Labels for break/continueSometimes we need to break out from multiple nested loops at once. For example, in the code below we loop over
We need a way to stop the process if the user cancels the input. The ordinary A label is an identifier with a colon before a loop:
The
In the code above, So the control goes straight from We can also move the label onto a separate line:
The Labels do not allow to “jump” anywhere Labels do not allow us to jump into an arbitrary place in the code. For example, it is impossible to do this:
A
…Although, 99.9% of the time A SummaryWe covered 3 types of loops:
To make an “infinite”
loop, usually the If we don’t want to do anything in the current iteration and would like to forward to the next one, we can use the
|