DEV Community

Cover image for Why map() Is Returning undefined in JavaScript (And How to Fix It)
Emily Scott
Emily Scott

Posted on

Why map() Is Returning undefined in JavaScript (And How to Fix It)

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);
Enter fullscreen mode Exit fullscreen mode

Expected Output

[2, 4, 6]
Enter fullscreen mode Exit fullscreen mode

Actual Output

[undefined, undefined, undefined]
Enter fullscreen mode Exit fullscreen mode

Very frustrating.


Why This Happens

When you use curly braces {} inside .map(), JavaScript expects an explicit return.

This code:

(num) => {
  num * 2;
}
Enter fullscreen mode Exit fullscreen mode

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;
});
Enter fullscreen mode Exit fullscreen mode

Output

[2, 4, 6]
Enter fullscreen mode Exit fullscreen mode

Option 2: Use Implicit Return

Cleaner version:

const doubled = numbers.map((num) => num * 2);
Enter fullscreen mode Exit fullscreen mode

This is shorter and commonly preferred.


The React Version of This Bug

This happens constantly in React:

users.map((user) => {
  <div>{user.name}</div>;
});
Enter fullscreen mode Exit fullscreen mode

Nothing renders.

Why?

Because JSX also needs return.

Correct Version

users.map((user) => {
  return <div>{user.name}</div>;
});
Enter fullscreen mode Exit fullscreen mode

Or:

users.map((user) => (
  <div>{user.name}</div>
));
Enter fullscreen mode Exit fullscreen mode

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;
});
Enter fullscreen mode Exit fullscreen mode

JavaScript thinks this is a function block, not an object.

Correct

const users = data.map((user) => ({
  name: user.name
}));
Enter fullscreen mode Exit fullscreen mode

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)