One of the most common JavaScript bugs developers face is using .map() and getting an array full of undefined.
It looks simple, but this issue wastes hours during debugging—especially in React projects, API handling, and frontend rendering.
Let’s fix it properly.
The Problem
You write code like this:
const numbers = [1, 2, 3];
const doubled = numbers.map((num) => {
num * 2;
});
console.log(doubled);
Expected Output
[2, 4, 6]
Actual Output
[undefined, undefined, undefined]
Very frustrating.
Why This Happens
When you use curly braces {} inside .map(), JavaScript expects an explicit return.
This code:
(num) => {
num * 2;
}
does not return anything.
And if nothing is returned, JavaScript automatically returns undefined.
That is why every item becomes undefined.
The Correct Fix
Option 1: Add return
const doubled = numbers.map((num) => {
return num * 2;
});
Output
[2, 4, 6]
Option 2: Use Implicit Return
Cleaner version:
const doubled = numbers.map((num) => num * 2);
This is shorter and commonly preferred.
The React Version of This Bug
This happens constantly in React:
users.map((user) => {
<div>{user.name}</div>;
});
Nothing renders.
Why?
Because JSX also needs return.
Correct Version
users.map((user) => {
return <div>{user.name}</div>;
});
Or:
users.map((user) => (
<div>{user.name}</div>
));
This is one of the most common frontend mistakes.
Another Hidden Trap
Returning objects also causes confusion.
Wrong
const users = data.map((user) => {
name: user.name;
});
JavaScript thinks this is a function block, not an object.
Correct
const users = data.map((user) => ({
name: user.name
}));
Wrap the object in parentheses.
Very important.
Quick Debug Rule
If .map() gives undefined, ask yourself:
“Did I actually return something?”
That question usually finds the bug immediately.
Final Thoughts
.map() is powerful, but tiny syntax mistakes create major confusion.
Remember:
- Curly braces need
return - Parentheses allow implicit return
- Objects need wrapping
() - React JSX must also return
Mastering this saves serious debugging time—especially in production apps.
Your Turn
What was your worst .map() debugging moment?
React developers usually have many.
Peace,
Emily Idioms
Top comments (0)