Add example to concisely return object (#20707)
* Add example to concisely return object This trick to directly and concisely return object might be useful for beginners * Change function name and logic Change function name and logic to make sense
This commit is contained in:
committed by
Manish Giri
parent
e4349263ae
commit
2e92a63a69
@ -40,6 +40,9 @@ const multiply = (x, y) => x * y;
|
||||
// if you only have one argument/parameter
|
||||
const multiplyBy2 = x => x * 2;
|
||||
|
||||
// if you need to concisely return an object, you can wrap the {} inside the () to avoid syntax conflicts
|
||||
const getSumProductObject = (x, y) => ({sum : x + y, product: x * y});
|
||||
|
||||
// combined with the ternary operator, but note it's not a looker!
|
||||
const addOrMultiply = (x, y, mathOperator) => mathOperator.toLowerCase() === 'add' ? x + y : x * y;
|
||||
```
|
||||
|
Reference in New Issue
Block a user