for
for(let i = 0; i < 3; i++) output.innerText += (i + "\n");
- loops over index
for in
var obj1 = {
"key1": "value1",
"key2": "value2"
};
for(let k in obj1)
output.innerText += (k + '\n');
- loops over keys of objects;
- in case of arrays, their numeric index.
for of
var arr1= ["element1", "element2"]; for(let el of arr1) output.innerText += (el + '\n');
- loops over values of objects;
- in case of arrays, their elements.
forEach
var arr2 = ["value3", "value4"];
function myFunc(element,index){
output.innerText += `${index} -> ${element}\n`;
}
arr2.forEach(myFunc);
- forEach is an Array prototypical method, accepting a function as argument.
- The function can have up to three parameters, which are the element, the index and the array itself respectively.