See this code:
<script>
let {foo} = null; // TypeError
</script>
<script>
// Here I want to assign some some value to foo
</script>
The first script attempts to let-declare foo
via a destructuring assignment. However, null
can't be destructured, so the assignment throws a TypeError.
The problem is that then the foo
variable is declared but uninitialized, so if in the 2nd script I attempt to reference foo
, it throws:
foo = 123; // ReferenceError: can't access lexical declaration `foo' before initialization
And let
variables can't be redeclared:
let foo = 123; // SyntaxError: redeclaration of let foo
Is there any way to take it out of the TDZ, so that I can assign values and read them?