Default value of destructured { o, b, j, e, c, t } = {}

# Bucaran (9 years ago)

If the last argument to a function is a destructured object with at least one having a default value, makie it {} by default.

For example:

function doSomething ({ a: true }) {
  console.log(a)
}
doSomething({ a: false }) // OK
doSomething() // fails because the object is undefined

// solution
function doSomething ({ a: true } = {}) {
  console.log(a)
}

I find that for complex objects (such as options to methods), having to make the argument’s default value {} in every function declaration inconvenient.

Let me know your feedback / comments.

Jorge Bucaran