A one liner arrow function will return the resulting value.
for instance:
const add = (a, b) => a + b;
Whereas a multiline arrow function (or one defined using {
& }
will not return a value:
const add = (a, b) => {
a + b;
};
in order for the above to return a value you will need the return keyword:
const add = (a, b) => {
return a + b;
};
The Confusing Part
Your case is a bit confusing because you're dealing with an object literal:
{ username: name }
this syntax represents an "object". Objects in javascript are similar to associative arrays in other languages - that is, they are like arrays with strings as indices. A common example you'll see is something like:
const person = { name: 'Joseph', age: 33 }
// output the name:
console.log(person.name);
// output the age:
console.log(person.age);
So by wrapping your object literal in parens in your fist example you maintain it as a one line arrow function and your object literal is returned. The second example is really a multiline definition, which will again... have no return value.