30 Days of Code: Common JavaScript Mistakes

🚀 30 Days — 30 Coding Mistakes Beginners Make Day 4/30 I returned a value inside `forEach()`… but my array became `undefined` 😐 const doubled = numbers.forEach(n => n * 2); The mistake: `forEach()` does NOT return anything. It only runs a loop. Correct 👇 const doubled = numbers.map(n => n * 2); `map()` creates a brand new array with transformed values. Simple rule: forEach → do something map → create something This is especially important in React when rendering lists. Day 5 tomorrow 👀 #30DaysOfCode #javascript #reactjs #frontend #webdevelopment #codeinuse

  • No alternative text description for this image

By the way, we have easier way to achieve the same goal by using double = numbers.map(n => n*2);

Like
Reply

To view or add a comment, sign in

Explore content categories