for of loop javascript mdn code example

Example 1: forloop in js

for(i = 0; i < x; ++i){
  //Loop, x can be replaced with any integer; 
}

Example 2: for of mdn

let iterable = new Uint8Array([0x00, 0xff]);

for (let value of iterable) {
  console.log(value);
}
// 0
// 255

Example 3: for of mdn

function* fibonacci() { // generatore di funzione
  let [prev, curr] = [0, 1];
  while (true) {
    [prev, curr] = [curr, prev + curr];
    yield curr;
  }
}

for (let n of fibonacci()) {
  console.log(n);
  // tronca la sequenza a 1000
  if (n >= 1000) {
    break;
  }
}

Example 4: use these instead of a for loop javascript

const array = [1,2,3,4,5];const evenNumbers = array.filter(function(elem) {   // expressions that return 'true' are retained   return elem % 2 == 0;});