Destructuring Assignment - Passing object as function's parameters

Hello all,

So I’m confused on how the code below works. How is it that the max and min objects passed into the half() function are assigned specifically to the respective values inside the stats object?

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

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

I figured out that this is the solution to the exercise (found in ES6 section), I just don’t understand how this works.

Any info will be very much appreciated!

-TR

Hello @tonatiuh.cuauhtemoc,

  • The function doesn’t actually automatically grab max and min from stats, if you check the challenge test on the sidebar, it is passed on the background, so you can’t see it but it’s happening behind the scenes.
    **I Have blurred the images because it contains the solution to the challenge **
  • If you want to actually see what’s happening in your code, you can use the console.log(); function. Console.logging is a very needed skill in JavaScript Programming. Here’s an example of what it looks like when you console.log(); the solution.
  • So if half(stats) is passed, that means {max, min} is going to grab the object value of max and min from stats and then do the math with it.
1 Like

Oh gosh, I do see that now!

And I will definitely be using console.log() as my trusted ally moving forward. Thank you for pointing that out!

And thank you for taking the time to answer my question. I very much appreciate it! :grinning: