Parameter Destructuring
Destructuring function parameters is idiomatic for options objects and React props — it documents the expected shape at the call site.
Destructuring function parameters is idiomatic for options objects and React props — it documents the expected shape at the call site.
function createUser({ name, email, role = "user", active = true } = {}) {
return { name, email, role, active };
}
createUser({ name: "Alice", email: "alice@example.com" });
// React-style
const Button = ({ label, onClick, disabled = false, className = "" }) => (
`<button class="${className}" disabled="${disabled}">${label}</button>`
);
Default the whole parameter to {} so calling with no arguments does not throw.
More in JavaScript