DEV Community

Carlos Daniel Ortega Hernandez
Carlos Daniel Ortega Hernandez

Posted on

Javascript Loops

For Loop

For loops are used to iterate over a list.

    for (var i = 0; i < 5; i++) {
        console.log(i);
    }
    // 0
    // 1
    // 2
    // 3
    // 4
Enter fullscreen mode Exit fullscreen mode

While Loop

While loop is used to execute a block of code as long as a condition is true.

    let i = 1;
    while (i < 5) {
        console.log(i);
        i++;
    }
    // 1
    // 2
    // 3
    // 4
Enter fullscreen mode Exit fullscreen mode

Do While Loop

Do While loops are similar to while loops, except that the condition is checked after the code block is run.

    let i = 1;
    do {
        console.log(i);
        i++;
    } while (i < 5);
    // 1
    // 2
    // 3
    // 4
Enter fullscreen mode Exit fullscreen mode

For In Loop

For in loops can be used to loop through the properties of an object.

    const array = [1, 2, 3, 4, 5];
    for (let i in array) {
        console.log(i);
    }
    // 0
    // 1
    // 2
    // 3
    // 4
Enter fullscreen mode Exit fullscreen mode

For Of Loop

For of loop is used to iterate over iterable objects like arrays and strings.

    const array = [1, 2, 3, 4, 5];
    for (let i of array) {
        console.log(i);
    }
    // 1
    // 2
    // 3
    // 4
    // 5
Enter fullscreen mode Exit fullscreen mode

For Each Loop

Traverse an array or object.

    const array = [1, 2, 3, 4, 5];
    array.forEach((element) => {
        console.log(element);
    })
    // 1
    // 2
    // 3
    // 4
    // 5
Enter fullscreen mode Exit fullscreen mode

Map Loop

Map loops are used to iterate over arrays and objects.

    const array = [1, 2, 3, 4, 5];
    const newArray = array.map((element) => {
        return element * 2;
    }
    console.log(newArray);
    // [2, 4, 6, 8, 10]
Enter fullscreen mode Exit fullscreen mode

Filter Loop

Returns a new array with all elements that pass the test implemented by the provided function.

    const array = [1, 2, 3, 4, 5];
    const newArray = array.filter((element) => {
        return element > 2;
    }
    console.log(newArray);
    // [3, 4, 5]
Enter fullscreen mode Exit fullscreen mode

Reduce Loop

Reduce is a loop that takes an array and reduces it to a single value.

    const array = [1, 2, 3, 4, 5];
    const newArray = array.reduce((acc, element) => {
        return acc + element;
    }
    console.log(newArray);
    // 15
Enter fullscreen mode Exit fullscreen mode

Find Loop

Returns the first element in the array that satisfies the provided testing function.

    const array = [1, 2, 3, 4, 5];
    const newArray = array.find((element) => {
        return element > 2;
    }
    console.log(newArray);
    // 3
Enter fullscreen mode Exit fullscreen mode

Find Index Loop

Returns the index of the first element in the array that satisfies the provided testing function.

    const array = [1, 2, 3, 4, 5];
    const newArray = array.findIndex((element) => {
        return element > 2;
    }
    console.log(newArray);
    // 2
Enter fullscreen mode Exit fullscreen mode

Every Loop

Returns true if all elements in the array pass the test. Or false if any element in the array fails the test.

    const array = [1, 2, 3, 4, 5];
    const newArray = array.every((element) => {
        return element > 2;
    }
    console.log(newArray);
    // false
Enter fullscreen mode Exit fullscreen mode

Some Loop

Returns true if any element in the array pass the test. Or false if all elements in the array fail the test.

    const array = [1, 2, 3, 4, 5];
    const newArray = array.some((element) => {
        return element > 2;
    }
    console.log(newArray);
    // true
Enter fullscreen mode Exit fullscreen mode

Top comments (0)