When You Want to Return Objects from an Arrow Function
- Christina Williams
- Aug 7, 2020
- 1 min read

One of the simplest and most standard ways of returning an object from an arrow function is to use the long-form syntax method. This is the most basic and common JavaScript.
For Example:
const createPizza = (name) => {
return {
name,
crust: thin
};
};
const pepperoni = createPizza('Pepperoni');
The above method is helpful if you need to add local variables above the return statement. However, it isn't practical if we do not want to declare any local variables but simply want to return an object.
For this purpose, we could use parentheses (). You are probably wondering how and where and what am I talking about. JavaScript has the ability to create expressions by using parentheses and when you wrap your intended object, creating an expression, you can then return an expression.
const createPizza = (name) => ({
name,
crust: thin
});
This is a nice little trick that only applies to return objects, but it is a nice little one for the books to remember when you want to write simpler code.



Comments