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

Hi All!

I have a question about this one…

Ok so I’ve solved the exercise and am all ready to move on! BUT I have a question about the function parameters.

We’re asked to send only max and min inside the function, which is fine! Buy my question is how does JS know which min and max to send through without referencing the stats object somehow?

Is it because the only object in the code contains these elements? If so, if we were to create another object that also had max and min elements, would this throw everything off?

If i’m understanding you correctly, you are asking how the js engine knows which stats object to reference for max and min?

First, you can declare a variable name only once, inside the scope. So you could name variables the same in different files and it would work, but highly not recommended. And it wouldn’t make sense if you’re naming your variable according to their usage.

But if you declare in the same scope they will fail

const var1 = ''

const var1 = {} // Error: variable is already declared

But you can re-assign a variable

// use let so that we can re-assign
let var1 = ''

// no need for declaration here (const, let, var)
var1 = {}

then the js engine follows your references. I numbered the order

const half = (function() {
  "use strict"; 

// #2
// when half is called, it will use whatever object you sent in.
// what you name the parameter does not have to match
// the name of the object, but anytime you want to reference
// that parameter, you have to use the same name
  return function half(customName) { 
    return (customName.max + customName.min) / 2.0;


// #1
// Here we send the reference to the stats object we want the result from

If there’s something I didn’t make clear, or misunderstood your question. Let me know.

1 Like

Really great explanation! Thank you very much!

I feel a bit silly now :hushed:

No need to feel silly. These things only seem obvious once you understand it. Up until that point, you’re banging your head on the table. I’ve been at this about 1.5 years and even I get confused over the simplest things still.