Control flow / Loops

let sum10 = 0;
for (let i = 1; i <= 10; i++) {
    sum10 += i;
}
//sum10 is 55

console.log(`sum10 is ${sum10}`);


The JavaScript for loop is similar to the Java and C for loop. A for statement looks as follows:

for (initialization; condition; afterthought)
  statement

1. The initializing expression initialization, if any, is executed. This expression usually initializes one or more loop counters, but the syntax allows an expression of any degree of complexity. This expression can also declare variables.
2. The condition expression is evaluated. If the value of condition is true, the loop statements execute. Otherwise, the for loop terminates. (If the condition expression is omitted entirely, the condition is assumed to be true.)
3. The statement executes. To execute multiple statements, use a block statement ({ }) to group those statements.
4. If present, the update expression afterthought is executed.
5. Control returns to Step 2.