I've read this trick in an answer here,
that it's possible to validate for the presence of foo
property like this:
const {foo = foo} = bar
It throws Uncaught ReferenceError: Cannot access 'foo' before initialization when foo
is undefined
.
Is this a well-defined behaviour in JS? Would this work reliably for all recent JavaScript versions and environments (browser, node) and likely future ones?
I am trying to see if it could make sense to start using this as a convenient shorthand validation if I don't care about having a more specific error message. Thanks.