Destructuring & Spread Operator
What happens when you try to destructure a null or undefined value?
When you try to destructure a null or undefined value, JavaScript throws a TypeError. This happens because destructuring attempts to access properties on the value being destructured, and accessing properties on null or undefined results in an error. For example, `const { prop } = null;` would throw "TypeError: Cannot destructure property 'prop' of 'null' as it is null". To guard against this error, you can provide a default empty object or array in the destructuring assignment: `const { prop } = obj || {};` or `const [item] = arr || [];`. Alternatively, you can use optional chaining in newer JavaScript versions: `const { prop } = obj?.someObj || {};`. This behavior makes sense from a safety perspective—it's typically better to fail fast with an error than to silently create variables with undefined values, which could lead to harder-to-detect bugs later in your code.