Iterable in JavaScript

In JavaScript, an iterable is an object that allows you to iterate over its elements using a loop or other built-in methods. Iterables are fundamental in modern JavaScript for working with collections of data. In this section, we'll explore what iterables are and how they work.

Basic Concepts of Iterable

An iterable is an object that implements the iterable protocol, which means it has a special method named Symbol.iterator. This method returns an iterator object, which is responsible for generating the sequence of values in the iterable. Iterators have a next() method that returns the next value in the sequence along with a boolean indicating if there are more values to iterate over.

Iterable Protocol

The iterable protocol consists of defining an object’s Symbol.iterator method, which returns an iterator object. This iterator object should have a next() method that returns an object with value and done properties. The value property represents the current value being iterated, and done indicates whether there are more values to iterate over.

Creating a Custom Iterable

Let’s create a custom iterable object that generates a sequence of Fibonacci numbers. We’ll implement the iterable protocol by defining a Symbol.iterator method and returning an iterator object.

				
					const fibonacci = {
  [Symbol.iterator]: function* () {
    let prev = 0, curr = 1;
    while (true) {
      yield curr;
      [prev, curr] = [curr, prev + curr];
    }
  }
};

const iterator = fibonacci[Symbol.iterator]();
console.log(iterator.next().value); // Output: 1
console.log(iterator.next().value); // Output: 1
console.log(iterator.next().value); // Output: 2
console.log(iterator.next().value); // Output: 3

				
			

In this example, we define a custom iterable fibonacci object with a Symbol.iterator method that generates Fibonacci numbers using a generator function (function*). We then create an iterator object using the Symbol.iterator method and iterate over the Fibonacci sequence using the next() method.

Advanced Concepts

Now, let’s delve into some advanced concepts related to iterables in JavaScript:

  • Iterables vs. Arrays: Understanding the difference between iterables and arrays and how they can be used interchangeably in many cases.
  • Iterable Methods: Exploring built-in methods like forEach(), map(), filter(), and reduce() that accept iterables as arguments and operate on their elements.
  • Custom Iterators: Creating custom iterators to control the iteration process and generate values dynamically.

Let’s demonstrate how to use iterable methods like forEach() and map() with arrays, which are iterables:

				
					const numbers = [1, 2, 3, 4, 5];

// Using forEach() to log each element
numbers.forEach(num => console.log(num)); // Output: 1 2 3 4 5

// Using map() to double each element
const doubled = numbers.map(num => num * 2);
console.log(doubled); // Output: [2, 4, 6, 8, 10]

				
			

In this example, we use the forEach() method to iterate over each element of the numbers array and log them to the console. We then use the map() method to create a new array doubled where each element is doubled.

Iterables are a fundamental concept in JavaScript for working with collections of data. By understanding the iterable protocol and how to create and use custom iterables, developers can efficiently work with sequences of values and leverage built-in methods for iteration and manipulation. Iterables play a crucial role in modern JavaScript programming, enabling developers to write more expressive and concise code. Happy coding !❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India