Destructuring & Spread Operator
What does the following code do? function fn({name, age}) { console.log(name, age); }
This code creates a function that logs name and age properties of any object passed to it. It uses object destructuring in the function parameter, which extracts the `name` and `age` properties from the object provided as an argument. For example, calling `fn({name: 'Alice', age: 30, job: 'Developer'})` would log `'Alice' 30` to the console. The function doesn't require the object to have only these properties—any additional properties will simply be ignored in the destructuring. If a property doesn't exist, its value will be `undefined`. This pattern is common in modern JavaScript, especially in React components and other frameworks, as it makes the function's expected input more explicit and reduces the need for repetitive property access inside the function body. It also allows for default values: `function fn({name = 'Unknown', age = 0} = {})` would provide defaults if properties are missing or if no object is passed at all.