Ticker

6/recent/ticker-posts

🔁 for...of Loop in JavaScript – The Loop That Finally Feels Clean

🔁 for...of Loop in JavaScript – The Loop That Finally Feels Clean

JavaScript looping used to feel unnecessarily annoying.

You had stuff like:

for (let i = 0; i < arr.length; i++) {
  console.log(arr[i]);
}

Works? Sure.

Fun to read? Absolutely not 😄

Then for...of came in and basically said:

👉 “bro… just give me the values directly”

And honestly? For most array iteration use cases, it’s way cleaner.

⚡ What for...of Actually Does

for...of loops through iterable values.

Meaning:

  • arrays
  • strings
  • maps
  • sets
  • NodeLists
  • generators

It gives you the actual value, not the index.

🔎 Basic Example

const fruits = ["apple", "banana", "orange"];

for (const fruit of fruits) {
  console.log(fruit);
}

Simple. Readable. No index nonsense.

🔥 Strings Work Too

const name = "JavaScript";

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

👉 Iterates character by character.

🧠 Why Devs Prefer It

Look at this:

for (let i = 0; i < users.length; i++) {
  console.log(users[i]);
}

vs

for (const user of users) {
  console.log(user);
}

Second one instantly tells you:

👉 “I care about the values, not indexes”

That clarity matters more than people think.

🔥 Real Use Case: Working with DOM Elements

const buttons = document.querySelectorAll("button");

for (const button of buttons) {
  button.disabled = true;
}

Very readable.

No converting NodeLists. No manual indexing.

💡 Real Developer Insight

Here’s the thing:

👉 for...of is not always the best choice.

If you need:

  • indexes
  • object keys
  • transformations

…there are better tools.

Example:

array.map()
array.entries()
Object.keys()

A lot of beginners try to force for...of everywhere.

Don’t.

Use it when you genuinely just want:

👉 “give me each value one by one”

⚠️ Important: for...of vs for...in

This confuses everyone initially.

for...of

👉 Gives values

const arr = ["a", "b"];

for (const value of arr) {
  console.log(value);
}

for...in

👉 Gives keys/indexes

for (const index in arr) {
  console.log(index);
}

Output:

0
1

👉 Don’t use for...in for arrays unless you specifically need indexes.

🔥 Need Both Index + Value?

Use .entries():

const fruits = ["apple", "banana"];

for (const [index, fruit] of fruits.entries()) {
  console.log(index, fruit);
}

This is a super clean pattern.

⚠️ Common Developer Mistakes

1️⃣ Using for...in instead of for...of for arrays
→ classic beginner confusion

2️⃣ Expecting it to work on plain objects

for (const item of obj) {} // ❌ error

Objects are not iterable by default.

3️⃣ Overusing loops when array methods fit better

Sometimes:

map()
filter()
reduce()

…are cleaner.

4️⃣ Mutating arrays while iterating

Yeah… things get weird fast.

🚀 Best Practice Summary

✅ Use for...of when you only need iterable values
✅ Use .entries() if you need both index and value
✅ Avoid for...in for array iteration
✅ Use array methods when transformation is needed
✅ Keep loops readable and purpose-driven

Reactions

Post a Comment

0 Comments