Why x => { x * 2 } Returns undefined: Arrow Function Basics
Arrow function syntax, one and many parameters, implicit vs explicit return, and how they differ from normal functions, with real output

This arrow function looks correct, and double(4) returns undefined:
const double = x => { x * 2 };
console.log(double(4));
undefined
Nothing is broken. The braces changed what the function does, and that one detail is the heart of arrow functions: a shorter way to write a function, with a few rules about return that you need to know. All outputs below are real runs on Node.js 24.22.1.
What arrow functions are
An arrow function is a function written with => instead of the function keyword. It does the same job with less to type. Here's a normal function that squares a number, next to two arrow versions:
function square(n) {
return n * n;
}
const squareArrow = (n) => {
return n * n;
};
const squareShort = n => n * n;
console.log(square(5), squareArrow(5), squareShort(5));
25 25 25
Three functions, one result. The last one dropped the function keyword, the braces and the word return. Less boilerplate means less to read, which is why modern JavaScript leans on them, especially for short functions passed to other functions.
Converting a normal function, step by step
Take the square function and change it one piece at a time:
- Remove the keyword
functionand the name. Store the function in aconstinstead. - Put
=>between the parameters and the body. - If the body is a single expression, drop the braces and
return.
Each step removes one piece: the keyword and name first, then the braces and return.
Each step gives you a working function. You can stop at step 2 and still be writing a normal, valid arrow function.
Syntax breakdown
Parameters on the left, the arrow in the middle, the result on the right.
An arrow function has three parts: the parameters on the left, the arrow =>, and the body on the right. The arrow reads as "takes these, gives back that".
One parameter, no parameters, many parameters
The parentheses around the parameters depend on how many you have:
const greet = name => `Hello, ${name}!`; // one parameter: parentheses optional
const add = (a, b) => a + b; // two or more: parentheses required
const sayHi = () => "Hi there"; // none: empty parentheses required
console.log(greet("Asha"));
console.log(add(2, 3));
console.log(sayHi());
Hello, Asha!
5
Hi there
My habit is to always write the parentheses, even for one parameter. It's one rule instead of three, and adding a second parameter later doesn't force an edit to the first. Both styles are valid, so follow whatever your team does.
Implicit return vs explicit return
This is the part that produced the undefined at the top. There are two ways to give a value back.
Implicit return has no braces. The expression after => is returned automatically:
const doubleImplicit = x => x * 2;
Explicit return uses braces and the return keyword. Braces open a function body, a block of statements. Nothing leaves a block unless you return it:
const double = x => { x * 2 }; // ❌ braces, no return
const doubleOk = x => { return x * 2; }; // ✅ braces and return
console.log(double(4));
console.log(doubleOk(4));
undefined
8
x * 2 was calculated and then thrown away, so the function returned undefined. The rule: braces mean you must write return; no braces means the value is returned for you. Use explicit return when the body needs more than one statement.
The object literal trap
Returning an object is where the two rules collide. JavaScript reads { after => as a function body, not an object:
const makeUserBad = name => { name: name }; // ❌ a body containing a label
const makeUser = name => ({ name: name }); // ✅ parentheses make it an object
console.log(makeUserBad("Asha"));
console.log(makeUser("Asha"));
undefined
{ name: 'Asha' }
Wrap the object in parentheses and JavaScript treats it as a value. It's the same braces problem as before, in a different outfit.
Arrow function vs normal function: the basics
Arrow functions are not only shorter. Three differences matter early on.
1. Syntax. Shorter, with implicit return available, as shown above.
2. You can't call them before their line has run. A function declaration can be used before the line that defines it. An arrow function stored in a const can't:
console.log(hello("Asha")); // works: declarations are hoisted
function hello(n) { return "Hello, " + n; }
try {
console.log(hi("Asha")); // fails: the const doesn't exist yet
} catch (e) {
console.log(e.name + ": " + e.message);
}
const hi = n => "Hi, " + n;
Hello, Asha
ReferenceError: Cannot access 'hi' before initialization
Strictly, this comes from const, not from the arrow: a normal function expression stored in a const (const g = function (n) { ... }) fails with the same ReferenceError when called early. I covered this in function declarations vs expressions.
3. They don't get their own this. I'm keeping this short on purpose. A normal function gets this from how it's called. An arrow function doesn't create one and uses whatever this surrounds it:
const user = {
name: "Asha",
regular() { return this.name; },
arrow: () => this.name,
};
console.log(user.regular());
console.log(user.arrow());
Asha
undefined
This ran as a CommonJS file in Node, where the outer this isn't user. The takeaway for now: don't use an arrow function as an object method. The full story is in how JavaScript decides what this is.
When not to use an arrow function: as an object method (above), and as a constructor, because new on an arrow function throws TypeError: Person is not a constructor. For short callbacks, use arrows freely.
The assignment, solved
1 and 2. Square: normal, then arrow. Both are in the first example, so here's the final form:
function square(n) {
return n * n;
}
const squareArrow = n => n * n;
3. Even or odd. One parameter, one expression, so an implicit return fits:
const isEvenOrOdd = n => (n % 2 === 0 ? "even" : "odd");
console.log(isEvenOrOdd(4), isEvenOrOdd(7));
even odd
n % 2 is the remainder after dividing by 2. Zero means even.
4. Arrow function inside map(). map runs a function on every item and collects the results in a new array. This is where arrows earn their place:
const isEvenOrOdd = n => (n % 2 === 0 ? "even" : "odd"); // from the previous example
const numbers = [1, 2, 3, 4, 5];
const squares = numbers.map(n => n * n);
console.log(squares);
const labels = numbers.map(n => isEvenOrOdd(n));
console.log(labels);
const withFunction = numbers.map(function (n) {
return n * n;
});
console.log(withFunction);
[ 1, 4, 9, 16, 25 ]
[ 'odd', 'even', 'odd', 'even', 'odd' ]
[ 1, 4, 9, 16, 25 ]
The first and third give identical results. One is a single short line, the other is four lines of ceremony.
Where this leaves you
Arrow functions cut the boilerplate for short functions. Remember one rule: braces need return, no braces returns for you. Wrap returned objects in parentheses.
This post skipped this on purpose, and arrow functions behave very differently there in more cases than the one I showed. I also haven't covered the arguments object.
Do you write the parentheses around a single parameter, or drop them? I'm curious whether anyone sticks with the short form.
Comments
Join the discussionNo comments yet. Be the first to comment.




