SyntaxStudy
Sign Up
JavaScript Beginner 3 min read

Destructuring in Loops

Destructuring in Loops

Destructure directly in for...of loops to extract values from arrays or Map entries cleanly.

Example
const users = [{ id: 1, name: "Alice" }, { id: 2, name: "Bob" }];
for (const { id, name } of users) {
  console.log(id, name);
}
const map = new Map([["a", 1], ["b", 2]]);
for (const [key, val] of map) {
  console.log(key, val);
}
Object.entries(obj).forEach(([k, v]) => console.log(k, v));
Pro Tip

Object.entries() + destructuring is the most readable way to iterate object key-value pairs.