I think you are asking about the right side of an arrow function.
It is just more concise, with the special rule that if the right side is a single expression to be returned, it doesn't have to be { return expr }
, but just expr
.
const sq = (x => x * x);
const hyphenAround = (s => `-${s}-`);
[1, 3, 5].map(a => a * a)
[1, 3, 5].reduce((a, b) => a + b)
const sq = (x => x * x);
const hyphenAround = (s => `-${s}-`);
console.log(sq(3));
console.log(hyphenAround("hello"));
console.log([1, 3, 5].map(a => a * a));
console.log([1, 3, 5].reduce((a, b) => a + b));
In your example, it is
const a = "hello"
const b = () => ({a})
which is the same as
const a = "hello"
const b = () => ({a: a})
Those are called shorthand property names.
let a = "hello"
const b = () => ({
a
});
console.log(b());
a = "a long sentence";
console.log(b());
x = 123;
y = "hello"
z = [1, 3, 5];
console.log({
x,
y,
z
});