---
title: "What is an iterable in JavaScript?"  
description: "What is an iterable in JavaScript?"  
author: "Revati S Misra"  
published: 2023-11-01  
updated: 2023-11-02  
canonical: https://www.mindstick.com/forum/160372/what-is-an-iterable-in-javascript  
category: "javascript"  
tags: ["javascript", "iterable"]  
reading_time: 2 minutes  

---

# What is an iterable in JavaScript?

What is an iterable in [JavaScript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript)?

## Replies

### Reply by Aryan Kumar

In JavaScript, an iterable is an object that defines how it can be iterated over. Iterables are a fundamental concept in modern JavaScript and are used for looping over the elements or values contained within the object. To be considered an iterable, an object must implement the iterator protocol.

The iterator protocol involves having a special method, **Symbol.iterator**, which returns an iterator object. This iterator object, in turn, should have a **next()** method that is used to retrieve the next value in the sequence.

Here's what defines an iterable in JavaScript:

**Symbol.iterator Method:** An iterable object must have a **Symbol.iterator** method. When called, this method returns an iterator object.

**Iterator Object:** The iterator object returned by **Symbol.iterator** should have a **next()** method. The **next()** method is used to retrieve the next value in the iteration.

**next() Method:** The **next()** method returns an object with two properties:

- **value**: This property contains the current value of the iteration.
- **done**: This property is a boolean that indicates whether the iteration is complete (**true**) or not (**false**).

Examples of built-in iterables in JavaScript include arrays, strings, maps, and sets. Here's how you can identify and use an iterable:

```plaintext
const array = [1, 2, 3];
const string = "Hello";
const map = new Map([["key1", "value1"], ["key2", "value2"]]);

for (const element of array) {
  console.log(element);
}

for (const char of string) {
  console.log(char);
}

for (const [key, value] of map) {
  console.log(key, value);
}
```

In these examples, **array**, **string**, and **map** are all iterable objects. You can use a **for...of** loop to easily iterate over their elements or key-value pairs. These objects adhere to the iterator protocol, which makes them iterable.


---

Original Source: https://www.mindstick.com/forum/160372/what-is-an-iterable-in-javascript

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
