JavaScript Data Structures and Algorithms ES6 14

Hello, so on the Java Data Structures and Algorithims stage, on the 14th part of the ES6 course where you are supposed to “Use Destructuring Assignment to Pass an Object as a Function’s Parameters”. I was very confused on why these two lines produce different results.

const half = ({max, min}) => (max + min) / 2.0;
const half = ({max, min}) => { (max + min) / 2.0};

The top one is correct and lets me pass, but the other does not give out the same answer. Why is that?

When you use curly braces on the right side of an arrow function then you must explicitly return a value with a return statement. Since you are not then the function returns undefined.


So an inline function with the arrow notation that has parenthesis only doesn’t require a return statement, but a destructured assignment inline function requires a return statement?

It has nothing to do with destructuring.

If you use { } after the => then you must explicitly write return and the value/expression.

For example, here are two ways of writing the same arrow function:

const addTwoNums = (num1, num2) => num1 + num2;


const addTwoNums = (num1, num2) => {
  return num1 + num2;

The equivalent of what you wrote is:

const addTwoNums = (num1, num2) => {
  num1 + num2;

The above function returns the value undefined as nothing was explicitly returned by the function.

Oh, ok. Thank you so much!