What will be the output of this code? function createUser(id, { name = 'Anonymous', age = 0 } = {}) { return { id, name, age }; } console.log(createUser(1));
The output will be `{ id: 1, name: 'Anonymous', age: 0 }`. This function demonstrates a common pattern for handling optional object parameters with defaults. When `createUser(1)` is called, only the `id` parameter receives a value. The second parameter is not provided, so its default value (an empty object `{}`) is used. Then, destructuring is applied to this empty object, with default values for the `name` and `age` properties. Since these properties don't exist in the empty object, their respective default values ('Anonymous' and 0) are used. This pattern is particularly useful for functions with many optional parameters, as it provides a clean interface while ensuring the function doesn't throw errors when properties are missing. The empty object default (`= {}`) is crucial—without it, passing no second argument would cause a TypeError when trying to destructure `undefined`.