I am online
← Back to Articles

Ways to Return an Object from an Arrow Function

JavaScriptApril 20, 2024

Learn a few different ways to return an object from an arrow function.

Sometimes you’ll just want to return an object and not use any local variables inside the function.

The most common and standard way of returning an object from an arrow function would be to use the longform syntax:

const createProduct = (name) => {
  return {
    name,
    price: 499,
  };
};

const item = createProduct("Battery");

// 'Battery'
console.log(item.name);

❌ Uncaught SyntaxError: Unexpected token ':'


const createProduct = (name) => {
  name,
  price: 499
};

👍 Perfect

const createProduct = (name) => ({
  name,
  price: 499,
});

That’s it. A really nice shorthand for returning objects from an arrow function.

Thanks for reading!