← #37 Create an object with dynamic keys
#38

Return an object in an arrow function quickly

Topics: JavaScript

If you want to return an object literal in an arrow function, just wrap the object in ():

// Doesn't work
const formatName = (first, last) => {
full: `${first} ${last}`,
short: `${first.charAt(0)} ${last.charAt(0)}`.toUpperCase(),
};

// Work
const formatName = (first, last) => ({
full: ...,
short: ...,
});

formatName('John', 'Doe'); // { full: 'John Doe', short: 'J D' }
Fix a typo or suggest an improvement
#39 Log a variable in an arrow function