Destructuring Assignment object + parameter

Hi so could anyone explain to me why the solution uses parentheses on return, but curly braces is wrong?

I sort of understand but it is confusing, I understand you don’t need any braces in an arrow function, but in the example here it does have curly braces {} ( so I guess if you want to return an object you add {}), and if you want to add two parameters you use (parentheses) ?

Ty :slight_smile:

WRONG:

{max + min} / 2.0; 
  **Your code so far**

const stats = {
max: 56.78,
standard_deviation: 4.34,
median: 34.54,
mode: 23.87,
min: -0.75,
average: 35.85
};

// Only change code below this line
const half = ({max,min}) => (max + min) / 2.0; 
// Only change code above this line
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36

Challenge: Use Destructuring Assignment to Pass an Object as a Function’s Parameters

Link to the challenge:

Parentheses () are being used because we are computing a single number with the math formula average = (minimum + maximum) / 2. Braces {} can’t be used to write math like this in Javascript. It is the same thing that was there before, just with destructuring in the arguments:

const half = (stats) => (stats.max + stats.min) / 2.0; 
1 Like

so the example that had curly braces on the return value, has nothing to do with the Destructuring destructor* ? yes?

Destructor is something completely different. That is not the word you think it is :upside_down_face:

The example did not have a return value anywhere in it.

const profileUpdate = ({ name, age, nationality, location }) => {

}

this one that I meant

Right. Me too. There is no return value whatsoever. That is an arrow function with an empty body and no return value.

That example just has a destructuring assignment within the argument list and an empty function body.

Yeah I meant there is an empty body on return value but it has {}, so what I am trying to understand does the empty {} have anything to do with the destructing itself.

You are using words that have meanings in very wrong ways, and this is causing a lot of confusion for you and me.

Destructuring:

const profileUpdate = (profileData) => {
  // This part below is destructuring
  const { name, age, nationality, location } = profileData;

}

Destructuring in the argument list:

// The part in the ()s is destructuring
const profileUpdate = ({ name, age, nationality, location }) => {

}

Function body:

const profileUpdate = ({ name, age, nationality, location }) => {
  // This is the function body
}

Return value:

const profileUpdate = ({ name, age, nationality, location }) => {
  // This is a return value
  return 0;
}

There is no ‘empty body on return value’. There just is no return value defined by this function.

The {} are defining the function body, but nothing is occurring inside of the {} in this example.


An arrow function does not always need {}s around the function body. If the result of the function bodycan be computed on a single line, then

const myFunc = (arg) => arg * 2;

is the same as

const myFunc = (arg) => {
  return arg * 2;
}

The first one (with no {}s around the function body and no return statement) is called an implicit return.

This gets a little funky when you are trying to implicitly return an object, but that is getting pretty far away from this challenge.


A destructor is something completely different that describes what happens when you ‘delete’ an object.

1 Like

I think I understand, the reason I asked is to understand the destructor syntax so I thought it had something to do with the return
I call whatever after => the return ( maybe it’s the wrong analogy)

The part after the => is not ‘the return’.

After the => comes the function body. The function body might be written with an implicit return (no {}s or return keyword) or it might not be. Either way, it is still the function body.

Ah okay, and if it has a single line ES6 allows to omit return and {}, to make a shorter body.

so for example, in this lesson if I wanted to add a variable after what was done then curly bracers were needed yes?
edit : yeah it is not that simple lol, I guess you have to write it differently if you have more than one line in the function body.

This is how you would do it if you wanted to put more lines.

1 Like

I tried it , with the return it does work , thanks a lot!

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.