Loops compared - Gosiaisean/arch GitHub Wiki

Syntax For For each map()
Syntax for ([initialExpression]; [conditionExpression]; [incrementExpression]) statement arr.forEach(callback(currentValue[, index[, array]]) {//do something }[, thisArg]); let newArray = arr.map(callback(currentValue[, index[, array]]) {// return element for newArray, after executing something}[, thisArg]);
Mutate array Yes No, but call back can No, but call back can
Return value none Undefined new Array
Promises yes Doesnt wait, expects synchronous yes
Can use break Yes No No
Chain No No Yes

For Loop

All three expressions in the head of the for loop are optional.

initialization

An expression (including assignment expressions) or variable declaration evaluated once before the loop begins. Typically used to initialize a counter variable. May optionally declare new variables with var or let keywords. Variables declared with var are not local to the loop, i.e. they are in the same scope the for loop is in. Variables declared with let are local to the statement.

condition

An expression to be evaluated before each loop iteration. If this expression evaluates to true, statement is executed. This conditional test is optional. If omitted, the condition always evaluates to true. If the expression evaluates to false, execution skips to the first expression following the for construct.

final-expression

An expression to be evaluated at the end of each loop iteration. This occurs before the next evaluation of condition. Generally used to update or increment the counter variable.

statement

A statement that is executed as long as the condition evaluates to true. To execute multiple statements within the loop, use a block statement ({ ... }) to group those statements. To execute no statement within the loop, use an empty statement (;).

For each

callback

Function to execute on each element. It accepts between one and three arguments: currentValue - The current element being processed in the array. index Optional - The index of currentValue in the array. array Optional - The array forEach() was called upon.

thisArg Optional

Value to use as this when executing callback.

map()

callback

Function that is called for every element of arr. Each time callback executes, the returned value is added to newArray. The callback function accepts the following arguments: currentValue The current element being processed in the array. indexOptional The index of the current element being processed in the array. arrayOptional The array map was called upon.

thisArgOptional

Value to use as this when executing callback.