Skip to content

Latest commit

 

History

History
74 lines (63 loc) · 3.4 KB

File metadata and controls

74 lines (63 loc) · 3.4 KB

Loops

How to Use Loops in JavaScript 10/13
Should You Stop Using .forEach() in Your JavaScript Code? 8/30
.filter() The filter() method creates a new array with all elements that pass the test implemented by the provided function.
.forEach() The forEach() method executes a provided function once for each array element.
.map() The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.
.reduce() The reduce() method executes a reducer function (that you provide) on each element of the array, resulting in a single output value.

Loops

const numbers = [2, 34, 3, 23, 42, 3, 1, 65, 364, 5, 645, 6];
const name = 'John Doe 🙋‍♂️';
const john = {
  name: 'John',
  age: 100,
  cool: true,
}

{% code title="for - loop" %}

for (let i = 0; i < numbers.length; i++) {
console.log(numbers[i]);
}

{% endcode %}

{% code title="for of - promises/await" %}

for(const letter of name) {
console.log(letter);
}

{% endcode %}

{% code title="for in - used for looping over keys of an object" %}

for(const prop in john) {
console.log(prop);
}

{% endcode %}

{% code title="while loop - runs until condition is false" %}

let cool = true;
let i = 1;
while(cool === true) {
  console.log('You are cool');
  i++;
  if(i > 100) {
    cool = false;
  }
}

{% endcode %}

{% code title="do while loop - will always run first do condition" %}

do {

} while () {

}

{% endcode %}