2
function getValues(param=[width, height]) {
   return `Width= ${width}, Height = ${height}`;
  }

The cod above doesn't work because "width" and "height" are not defined within the function body

But, the function definition below works fine and I am not quite clear why the array parameters are automatically destructured to its elements below, while it doesn't so in the above case.

 function getValues([width, height]) {
   return `Width= ${width}, Height = ${height}`;
  }

I would appreciate for any clarification here. Thank you.

3 Answers 3

1

Using = in arguments, is basically setting default value in case of missing parameter (Default parameters). Once, you call the function with missing parameter(s) or undefined as parameter, default value is set against the argument. In your example, if you pass any argument, the default value [width, height] will be ignored and if you do not, Uncaught ReferenceError will be thrown as width and height does not exist (provided not defined globally or in the function scope).

The following will give you value for width and height as the first parameter is assigned to width and second to height.

function getValues(width, height, param=[width, height]) {
   console.log(param); // [5,6]
   return `Width= ${width}, Height = ${height}`;
}
  
console.log(getValues(5,6));

But, the function definition below works fine and I am not quite clear why the array parameters are automatically destructured to its elements below, while it doesn't so in the above case.

This is in-accordance to the rules of Destructuring. Where if a parameter is an array, you can assign them into different variables based on index. e.g. here the first value in the array will be assigned to width and the second value will be assigned to height.

function getValues([width, height]) {
  return `Width= ${width}, Height = ${height}`;
}

console.log(getValues([5, 6]));

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, your statement = in arguments, is basically setting default value clarifies half of my confusion. would you comment on my conclusion Array elements in the function argument are always destructured and available in the function definition as variables. Would this be valid conclusion? @Nikhil Aggarwal
They are always available and CAN BE destructured if required.
0

Both functions are fine. In the second one the first value of the array is assigned to the width variable, and the second to the height variable in the function scope. Remember to call the function using an array.

function getValues([width, height]) {
  return `Width= ${width}, Height = ${height}`;
}

getValues([100,200]); // 'Width= 100, Height = 200'

1 Comment

Ahh.. My mistake.. It would if the Ramesh's answer was used.
0
  • first function have array values in assigned to the param function parameter
  • Second function have only the parameter as values.

You should try

function getValues(param=[width, height]) {
   return `Width= ${param[0]}, Height = ${param[1]}`;
  }

1 Comment

Yes, your code works fine, I have no issue with that. But, why array destructuring is not happening here, just like my second code? I am trying to understand when array destructuring happens in the function parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.