Javascript for-of Statement

Introduction

The for-of statement is a strict iterative statement.

It is used to loop through elements in an iterable object.

Here's the syntax:

for (property of expression) 
   statement 

And here's an example of its usage:

for (const el of [2, 4, 6, 8]) {
    console.log(el);
}

Here, the for-of statement is used to display all the elements inside the four-element array.

The const operator in the control statement is not necessary but is recommended.

The for-of loop will iterate in the order that the iterable produces values via its next() method.

The for-of statement will throw an error if the entity does not support iteration.




PreviousNext

Related