Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

JS Iterables

Iterables are objects that can be iterated over (such as Arrays).

They can be accessed using straightforward and efficient code.

You can use for…of loops to iterate over iterables.

The For Of Loop

The JavaScript for…of statement iterates over the elements of an iterable object.

Syntax

for (variable of iterable) {
  // code block to be executed
}

Iterating

Iterating is straightforward; it refers to looping through a sequence of elements.

Here are some simple examples:

  • Iterating over a String
  • Iterating over an Array

Iterating Over a String

You can utilize a for…of loop to iterate through the characters of a string.

Example

const name = “W3Schools”;

for (const x of name) {
  // code block to be executed
}

Iterating Over an Array

You can use a for…of loop to iterate through the elements of an array.

Example 1

const letters = [“a”,“b”,“c”];

for (const x of letters) {
  // code block to be executed
}

Example 2

const numbers = [2,4,6,8];

for (const x of numbers) {
  // code block to be executed
}