I'm trying to wrap my head around destructured parameter default value assignments in JavaScript. I found this example:
```javascript
function preFilledArray([x = 1, y = 2] = []) {
return x + y;
}
```
When using this function:
- `preFilledArray()` returns 3
- `preFilledArray([])` returns 3
- `preFilledArray([2])` returns 4
- `preFilledArray([2, 3])` returns 5
I don't quite understand if this is something that can be logically comprehended from a development perspective or if it's more about memorizing how it works.
I've been exploring documentation and asking AI, but it's just not clicking for me. It seems like there are two aspects to this concept:
1. Destructuring with defaults: `[x = 1, y = 2] = arr`
2. Setting parameter defaults: `function fn(param = defaultValue)`
From my understanding, `param` represents `arr`, which means `[]` is the default value. However, it feels like `[x = 1, y = 2]` serves as both the default value and the parameter.
Why don't we see a structure like `function preFilledArray([x = 1, y = 2] = arr)` or simply `function preFilledArray([x = 1, y = 2])`? I'm hoping for an explanation that might present a new perspective.
5 Answers
Don’t think of it as overly complicated. You’re just merging a few JavaScript features:
- Optional function parameters.
- Destructuring.
- Default values.
It’s essentially a way to make parameters flexible – they can be absent, short, or long, and your function still knows how to handle them without breaking. Just remember, you need that `= []` to make the function safe against undefined situations!
Another perspective is that if you had simply used `function preFilledArray([x = 1, y = 2])` without the array default, you'd end up getting an error with calls like `preFilledArray()` or `preFilledArray(undefined)`. The `= []` serves to protect you from such scenarios, allowing the defaults for x and y to kick in when necessary.
The reason for using `function preFilledArray([x = 1, y = 2] = [])` is to prevent errors when no argument is passed or if `undefined` is provided. If you just wrote `function preFilledArray([x = 1, y = 2])`, calling it without an argument would throw an error since it can't destructure `undefined`. Using `[]` as a default value allows the function to run without crashing.
Using objects instead of arrays can also clarify this concept! For example, consider a function that accepts an object with default values. It can make things clearer how defaults work in destructuring. If you want to grasp this better, try working through similar examples using both arrays and objects to see how they handle defaults differently!
To break it down, when you define `([x = 1, y = 2] = [])`, it works like this:
1. You expect to receive an array.
2. By destructuring, you're saying you only care about the first two elements.
3. Providing defaults for x and y ensures they get assigned if no valid values are passed.
4. Finally, by defaulting the array itself (`= []`), you avoid issues if no argument is passed at all.
Neglecting to set the array default would result in an error if the function is called without arguments.
Exactly! You've summed it up well. Without that array default, trying to destructure from `undefined` would lead to errors. It's a neat way to ensure your function stays robust.

I think the confusion lies in how `undefined` interacts with destructuring. When you call `preFilledArray()`, it leads to `[x, y] = undefined`, which is problematic. To solve this, the default `[]` ensures that you effectively get `[x = 1, y = 2]`, allowing you to avoid any errors.