Javascript Loops
Tags: javascript
Published: 2022-05-12 23:44:36

## For Loop
For loops are used to iterate over a list.
```javascript
    for (var i = 0; i < 5; i++) {
        console.log(i);
    }
    // 0
    // 1
    // 2
    // 3
    // 4
```

## While Loop
While loop is used to execute a block of code as long as a condition is true.
```javascript
    let i = 1;
    while (i < 5) {
        console.log(i);
        i++;
    }
    // 1
    // 2
    // 3
    // 4
```

## Do While Loop
Do While loops are similar to while loops, except that the condition is checked after the code block is run.
```javascript
    let i = 1;
    do {
        console.log(i);
        i++;
    } while (i < 5);
    // 1
    // 2
    // 3
    // 4
```

## For In Loop
For in loops can be used to loop through the properties of an object.
```javascript
    const array = [1, 2, 3, 4, 5];
    for (let i in array) {
        console.log(i);
    }
    // 0
    // 1
    // 2
    // 3
    // 4
```

## For Of Loop
For of loop is used to iterate over iterable objects like arrays and strings.
```javascript
    const array = [1, 2, 3, 4, 5];
    for (let i of array) {
        console.log(i);
    }
    // 1
    // 2
    // 3
    // 4
    // 5
```

## For Each Loop
Traverse an array or object.
```javascript
    const array = [1, 2, 3, 4, 5];
    array.forEach((element) => {
        console.log(element);
    })
    // 1
    // 2
    // 3
    // 4
    // 5
```

## Map Loop
Map loops are used to iterate over arrays and objects.
```javascript
    const array = [1, 2, 3, 4, 5];
    const newArray = array.map((element) => {
        return element * 2;
    }
    console.log(newArray);
    // [2, 4, 6, 8, 10]
```

## Filter Loop
Returns a new array with all elements that pass the test implemented by the provided function.
```javascript
    const array = [1, 2, 3, 4, 5];
    const newArray = array.filter((element) => {
        return element > 2;
    }
    console.log(newArray);
    // [3, 4, 5]
```

## Reduce Loop
Reduce is a loop that takes an array and reduces it to a single value.
```javascript
    const array = [1, 2, 3, 4, 5];
    const newArray = array.reduce((acc, element) => {
        return acc + element;
    }
    console.log(newArray);
    // 15
```

## Find Loop
Returns the first element in the array that satisfies the provided testing function.
```javascript
    const array = [1, 2, 3, 4, 5];
    const newArray = array.find((element) => {
        return element > 2;
    }
    console.log(newArray);
    // 3
```

## Find Index Loop
Returns the index of the first element in the array that satisfies the provided testing function.
```javascript
    const array = [1, 2, 3, 4, 5];
    const newArray = array.findIndex((element) => {
        return element > 2;
    }
    console.log(newArray);
    // 2
```

## Every Loop
Returns true if all elements in the array pass the test. Or false if any element in the array fails the test.
```javascript
    const array = [1, 2, 3, 4, 5];
    const newArray = array.every((element) => {
        return element > 2;
    }
    console.log(newArray);
    // false
```

## Some Loop
Returns true if any element in the array pass the test. Or false if all elements in the array fail the test.
```javascript
    const array = [1, 2, 3, 4, 5];
    const newArray = array.some((element) => {
        return element > 2;
    }
    console.log(newArray);
    // true
```