✅let can't shadow parameter even in sloppy mode❗️
JS ⟩ variable ⟩ shadowing ⟩ let can't shadow parameter even in sloppy mode.
'use strict'; // ⭐ toggle sloppy/strict mode
const { log } = console;
// ⭐ only "simple" parameters
// "parameters" / "body declarations" in the same environment record.
function f(x) {
let x = 1; // ⛔ SyntaxError
log(x);
}
// ⭐ "non-simple" parameters exist,
// "parameters" / "body declarations" in different environment records.
function g(x = 0) {
let x = 100; // ⛔ SyntaxError
log(x);
}
function h(x = 0) {
var x = 300; // ❗ "var" do shadow parameter (even in strict mode)
log(x);
}
function k(x) {
var x = 400; // ❗ "var" do shadow parameter
log(x);
}
// mode: sloppy strict
// -----------------------------------------------
f(10); // ⛔ SyntaxError ⛔ SyntaxError
g(20); // ⛔ SyntaxError ⛔ SyntaxError
h(30); // ✅ 300 ✅ 300
k(40); // ✅ 400 ✅ 400
// ⛔ SyntaxError: Identifier 'x' has already been declared