Destructuring & Spread Operator
What will be the value of x and y after this code? let [x = 1, y = 2] = [undefined, null];
After this code runs, the values will be `x = 1` and `y = null`. This demonstrates how default values work in destructuring assignments. Default values are only applied when the corresponding value in the array is `undefined` or when the position doesn't exist in the array. In this case, the first element of the array is `undefined`, so the default value of 1 is assigned to `x`. The second element is `null`, which is a defined value in JavaScript (not `undefined`), so the default value is not used and `null` is assigned to `y`. This behavior is important to understand: default values in destructuring are not applied for all falsy values—only for `undefined`. Values like `null`, `0`, empty strings, and `false` will override the default values because they are considered defined values. This allows for precise control over which values trigger defaults versus which ones are passed through as-is.