A one liner (not a code block) is just that - one line of code and whatever it evaluates to is what it returns.
const foo = param => bar(param);
When foo is called, it will pass param to bar and whatever bar returns, it will pass through and be what foo returns. This is functionally equivalent to this:
const foo = param => {
const returnValue = bar(param);
return returnValue;
};
which in roughly equivalent to:
function foo(param) {
const returnValue = bar(param);
return returnValue;
}
Or of course in the last two examples:
const returnValue = bar(param);
return returnValue;
could be simplified to:
return bar(param);
That is it. That is the difference. There are technically a few other small differences between an arrow function and a standard function, but it doesn’t matter in this example.
Is it true if I say that { x, y } is inevitably/ obligatorily an object and never 2 intructions( codeblock) ?
It depends what you want. This:
const foo = (x, y) => { x, y };
is perfectly valid JS, it just doesn’t do what you want. This is the same as:
const foo = (x, y) => {
x, y;
};
If I call it with foo(1, 2);
, then it is equivalent to this:
const foo = () => {
1, 2;
};
Since the comma operator evaluates the expressions and uses the last one, it is the equivalent to this:
const foo = () => {
2;
};
You are not doing anything with that expression - you are not returning it, you are not using it in another expression, you are not assigning it to a variable. It is just an expression in the middle of nowhere that gets lost because we are not using it. It is a tree falling in the forest where there is no one there to hear it. It gets lost forever. And since we are not returning anything, the function does nothing and returns undefined
.
From that you said it seem it’s clear that is an objet. But when I read what Reaz says I’m not sure anymore.
This does nothing but returns an object:
const foo = (x, y) => ({ x, y });
This does nothing and returns nothing:
const foo = (x, y) => { x, y };
As far as what Raez said:
… curly brackets expanding the arrow callback one line to multiline where you need to use return statement to get back any value.
That is consistent with what I am saying. Curly braces will make it a code block so you can have more than one line of code.
If you still want use one line return then the return statement have to be ommited …
Yes, I agree with that. If you want to have one line, then omit the curly braces so it knows that it is not a code block and you don’t need a return
- whatever that expression/statement evaluates to will be the return value.
… and curly brackets have to be covered by parentheses.
In the context of “wanting to return an object”, yes, that is correct. If you weren’t returning an object it wouldn’t matter:
const sum = (x, y) => x + y;
It is not needed there because the is no ambiguity. But if you are trying to return an object with the one line, implicit return, when JS sees that {
it cannot tell if you are defining an object literal or if you are starting a code block. It will assume you are making a code block. If you want to tell it that you are writing an object literal, then do as Raez says and wrap it in parentheses.