JavaScript Iterator

Introduction

An /iterator/ is a pointer for traversing the elements of a data structure.

Examples

Syntax

const iterable = [“a”, “b”];
const iterator = iterableSymbol.iterator;

console.log(iterator.next()); // { value: ‘a’, done: false }
console.log(iterator.next()); // { value: ‘b’, done: false }
console.log(iterator.next()); // { value: undefined, done: true }

TypeScript Interfaces

Expressed as [TypeScript Interfaces](TypeScript Interfaces) in TypeScript notation:

interface Iterable {
    [Symbol.iterator]() : Iterator;
}
interface Iterator {
    next() : IteratorResult;
}
interface IteratorResult {
    value: any;
    done: boolean;
}