📘arr.forEach()

🚧 施工中

JSsyntaxfor loops ⟩ arr.forEach()

  • to skip a case, use return.

  • ⭐️ there's no way to break/continue in this method❗️

💾 程式:replit

let array = [1, 2, 3, 4, 5];

// ⭐️ skip a case with `return`
array.forEach(n => {
    if (n % 2 !== 0) return;        // ✅ use `return`
    console.log(n);                 // 2, 4
});

// ⭐️ skip cases with `filter()`
array
    .filter(n => n % 2 == 0)        // ✅ use `filter`
    .forEach(n => console.log(n));  // 2, 4aa

Last updated