1
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?

wisnia
  • 75
  • 2
  • 8

1 Answers1

2

undefined is no object.

You could use a default object as well.

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

f();
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392