Languages
[Edit]
EN

JavaScript - for...of statement

0 points
Created by:
Elias999
819

In this article, we would like to show you how to use for...of statement in JavaScript.

Description

The for...of statement creates a loop iterating over iterable objects (such as strings, arrays, maps, sets, and user-defined iterables).

Syntax

for (variable of iterable) {
  // statement
}

Practical examples

Example 1 - iterating over a string

// ONLINE-RUNNER:browser;

var iterable = 'ABC';

for (const value of iterable) {
  console.log(value);
}

Output:

A
B
C

Example 2 - iterating over an array

// ONLINE-RUNNER:browser;

let array = [1, 2, 3, 'text'];

for (let entry of array) {
  console.log(entry);
}

Output:

1
2
3
text

Example 3 - iterating over a map

// ONLINE-RUNNER:browser;

const iterable = new Map([
  ['A', 1],
  ['B', 2],
  ['C', 3],
]);

for (const value of iterable) {
  console.log(value);
}

Output:

A,1
B,2
C,3

References

  1. for...of - JavaScript | MDN

Alternative titles

  1. JavaScript - for of loop
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

JavaScript - Statements & declarations

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join