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 Loop For Of in JavaScript

Js Loop For Of in JavaScript

? JavaScript for...of Loop

The for...of loop is used to iterate over iterable objects like arrays, strings, maps, sets, and more. It directly gives you the values of the iterable instead of the index.


? Syntax

for (const element of iterable) {  // code to execute using element}

? Example: Loop through an Array

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

Output:

applebananacherry

? Loop through a String

const word = "Hello";for (const char of word) {  console.log(char);}

Output:

Hello

? Benefits of for...of

  • Easier syntax when you only need the values (no index)

  • Works on any iterable (arrays, strings, sets, maps, NodeLists, etc.)

  • Cleaner and less error-prone than traditional for loops for iteration


? Difference Between for...of and for...in

for...offor...in
Iterates over valuesIterates over keys/indexes
Works on iterables like arrays, stringsWorks on objects’ enumerable properties
Use to get actual dataUse to get property names or indexes

Would you like an example of for...in or more advanced iteration?

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