There was a time when I used and understood the practical use of let, const, and var in JavaScript, but explaining it in words was challenging. If you find yourself in a similar predicament the key points to focus on are the differences in scope, hoisting, reinitialization, and reassignment.
Scoping:
function varExample() { if (true) { var x = 10; // x is function-scoped } console.log(x); // Outputs: 10 } varExample(); if (true) { var y = 20; // y is globally scoped because it's outside a function } console.log(y); // Outputs: 20
function letExample() { if (true) { let x = 10; // x is block-scoped console.log(x); // Outputs: 10 } console.log(x); // ReferenceError: x is not defined } letExample(); if (true) { let y = 20; // y is block-scoped console.log(y); // Outputs: 20 } console.log(y); // ReferenceError: y is not defined
function constExample() { if (true) { const x = 10; // x is block-scoped console.log(x); // Outputs: 10 } console.log(x); // ReferenceError: x is not defined } constExample(); if (true) { const y = 20; // y is block-scoped console.log(y); // Outputs: 20 } console.log(y); // ReferenceError: y is not defined
Hoisting
Hoisting is like setting up a workspace before you start a task. Imagine you’re in a kitchen, preparing to cook a meal. Before you start cooking, you place all your ingredients and tools on the counter so they're within reach.
In programming, when you write code, the JavaScript engine goes through your code before actually running it and sets up all the variables and functions at the top of their scope. This means that you can use functions and variables before you've declared them in your code.
All three (var, let, and const) are indeed hoisted. However, the difference lies in how they are initialized during the hoisting process.
var is hoisted and initialized with undefined.
console.log(myVar); // Outputs: undefined var myVar = 10;
console.log(myLet); // ReferenceError: Cannot access 'myLet' before initialization let myLet = 10;
console.log(myConst); // ReferenceError: Cannot access 'myConst' before initialization const myConst = 20;
Reassignment and Reinitialization:
var x = 10; x = 20; // Reassignment console.log(x); // Outputs: 20 var x = 30; // Reinitialization console.log(x); // Outputs: 30
let y = 10; y = 20; // Reassignment console.log(y); // Outputs: 20 let y = 30; // SyntaxError: Identifier 'y' has already been declared
const z = 10; z = 20; // TypeError: Assignment to constant variable. const z = 30; // SyntaxError: Identifier 'z' has already been declared
const obj = { a: 1 }; obj.a = 2; // Allowed, modifies the property console.log(obj.a); // Outputs: 2 obj = { a: 3 }; // TypeError: Assignment to constant variable.
const arr = [1, 2, 3]; arr[0] = 4; // Allowed, modifies the element console.log(arr); // Outputs: [4, 2, 3] arr = [5, 6, 7]; // TypeError: Assignment to constant variable.
The above is the detailed content of An Overview of Let, Const, and Var: Key Differences Explained. For more information, please follow other related articles on the PHP Chinese website!