3 ms·
Why wouldn't `let` be exactly what you want? It's block scoped but doesn't need fancy TDZ checks because like `var` it just starts out as undefined.
by conartist6 1y ago
Why wouldn't `let` be exactly what you want? It's block scoped but doesn't need fancy TDZ checks because like `var` it just starts out as undefined.
- pverheggen 1y agoI think it’ll still throw a ReferenceError. Initialization is optional, but you still have to initialize before referencing.
- rictic 1y agoNope. `(() => {let bar; return bar})()` is `undefined`
- deleted 1y ago[deleted]
- strken 1y agoThe article is slightly wrong. The TDZ is the zone before the variable is declared, not after. Referencing variables before they're declared isn't valid for `let` and hence it needs a TDZ check. Consider console.log(foo) let foo vs console.log(foo) var foo I think the article confuses "in scope" with "declared", and "declared and initialised" with "initialised".