declares a block-scoped local variable.
Last updated 2 years ago
Was this helpful?
JS ⟩ variable ⟩ let
JS ⟩ declaration ⟩ variable ⟩ let
⭐️ ES6 (2015)
(declaration) declares a local variable in block scope.
⛔ ReferenceError: cannot access '...' before initialization.
typeof let/const/class in TDZ gets an error❗️
❗global let shadows global object property❗
✅let redeclaration not allowed even in sloppy mode❗️
✅let can't shadow parameter even in sloppy mode❗️
let is
a contextual keyword,
not reserved word❗
JS is lenient with var, strict with let.
var redeclaration applied even in strict mode❗️, even in strict mode❗
let redeclaration not allowed even in sloppy mode❗️, even in sloppy mode❗
雖說似乎有一個奇怪的 "for-init" scope,但裡面 let 變數的表現其實更像是 block-scoped。👉 scopes matter with closures ⟩
⚖️const / var
⛔temporal dead zone❗
❗var can shadow parameter even in strict mode❗️
❗var redeclaration applied even in strict mode❗️
YDKJS: Scope & Closures (v.2) ⟩ Ch. 3: The Scope Chain
Statements and declarations ⟩ declaring variables ⟩ let