Search code examples
javascriptecmascript-6argumentsarrow-functionsdestructuring

JavaScript - default values in destructured object passed to arrow function as an argument


const f = ({ a: a = 0, b: b = 1 }) => { ... }

I'm looking for an explanaition to why this doesn't work when I call f() and it does when I call f({}).

Main question: Is it possible to have an arrow function with destructured object passed as an argument, with it's properties being given default values if not defined?


Solution

  • undefined is no object.

    You could use a default object as well.

    const f = ({ a = 0, b = 1 } = {}) => { console.log(a, b) };
    
    f();