Foreach through all array elements in JavaScript

You may not know, but JavaScript allows us to use foreach loop. It’s not that popular, but it works and get the job done.

Here’s how to use it:

<script>
    array.forEach(element => {
        // do something with element
    });
</script>

Example:

Print every array element in console with foreach loop.

<script>
    array = ['dog', 'elephant', 'cat', 'mouse'];
    
    array.forEach(element => {
        console.log(element);
    });
</script>

Output:

Console Output