🔰initial value
🚧 施工中
variable declaration can assign a value to a variable.
let i = 0, j = 0, k = 0; // ⭐ in a single declaration
let [u, v] = [3, 4]; // 💡 destructuring assignment
let x = 2, y = x * x; // ✅ use "previously declared"
let [p, q, ...rest] = [10, 20, 30, 40, 50]; // "rest" operator
// ❌ "destructuring assignment" can't use "previous element"
let [a, b] = [ 2, a * a ]; // ⛔ ReferenceErrorconst must be declared with an initial value❗
var hoisting - declaration hoisted, value initialized to "undefined".
let/const/class hoisting - declaration hoisted, value "uninitialized"❗️
const requires initialization - must have with initial value❗️
function hoisting - declaration hoisted, value initialized to a function.
replit:initial value
let message = "hello";
// 💡 declare multiple variables
let i = 0, j = 0, k = 0; // ⭐ in a single declaration
let [u, v] = [3, 4]; // ⭐ destructuring assignment
// 💡 destructuring assignment with "rest" operator
let [p, q, ...rest] = [10, 20, 30, 40, 50];
// ✅ initializers can use "previously declared variables"
let x = 2, y = x * x;
// ❌ but no luck with "destructuring assignment"
// -----------------------------------------------------------
let [a, b] = [ 2, a * a ]; // ⛔ ReferenceError
// ^
// ⛔ ReferenceError: Cannot access 'a' before initialization
// -----------------------------------------------------------👉 ReferenceError: cannot access 'xxx' before initialization❗️
hoistingwill / won't provide initial value for (var|function) / (let|const) declaration.
default parameter uses initial values.
uninitialized variable has no initial values, not undefined even❗
destructuring assignment can be used to provide initial values.
Last updated
Was this helpful?