Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Js Iterables in JavaScript

Js Iterables in JavaScript

? JavaScript Iterables

Iterables are objects that can be looped over (iterated) using constructs like for...of. This includes data structures such as arrays, strings, maps, sets, and more.


? What Is an Iterable?

An iterable is any object that implements the Symbol.iterator method and returns an iterator.


? Common Built-in Iterables:

Iterable TypeExample
Array[1, 2, 3]
String"hello"
Setnew Set([1, 2, 3])
Mapnew Map([[1, "a"]])
NodeListdocument.querySelectorAll("p")

? Example 1: Iterating an Array

const fruits = ["apple", "banana", "cherry"];for (let fruit of fruits) {  console.log(fruit);}

? Example 2: Iterating a String

const text = "JS";for (let char of text) {  console.log(char);  // J then S}

? Example 3: Custom Iterable

You can create your own iterable object:

const myIterable = {  *[Symbol.iterator]() {    yield 1;    yield 2;    yield 3;  }};for (let value of myIterable) {  console.log(value);}

? Differences: for...of vs for...in

for...offor...in
Iterates values of iterablesIterates keys of objects (or arrays)
Used with arrays, strings, etc.Used with objects

? Use With Spread and Destructuring

const nums = [1, 2, 3];const copy = [...nums]; // Spread operator with iterableconsole.log(copy); // [1, 2, 3]

Would you like examples of iterating Maps, Sets, or creating your own iterator?

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql