Các biến được khai báo bằng từ khóa
[[] => {
var functionScopedVariable = 42;
let blockScopedVariable = 43;
console.log[functionScopedVariable]; // 42
console.log[blockScopedVariable]; // 43
}][];
console.log[functionScopedVariable]; // ReferenceError: functionScopedVariable is not defined
console.log[blockScopedVariable]; // ReferenceError: blockScopedVariable is not defined
1 thuộc phạm vi khối, có nghĩa là chúng chỉ khả dụng trong khối mà chúng được khai báoỞ cấp cao nhất [bên ngoài chức năng]
Ở cấp cao nhất, các biến được khai báo bằng cách sử dụng
[[] => {
var functionScopedVariable = 42;
let blockScopedVariable = 43;
console.log[functionScopedVariable]; // 42
console.log[blockScopedVariable]; // 43
}][];
console.log[functionScopedVariable]; // ReferenceError: functionScopedVariable is not defined
console.log[blockScopedVariable]; // ReferenceError: blockScopedVariable is not defined
1 không tạo thuộc tính trên đối tượng toàn cầuvar globalVariable = 42;
let blockScopedVariable = 43;
console.log[globalVariable]; // 42
console.log[blockScopedVariable]; // 43
console.log[this.globalVariable]; // 42
console.log[this.blockScopedVariable]; // undefined
Bên trong một chức năng
Bên trong một chức năng [nhưng bên ngoài một khối],
[[] => {
var functionScopedVariable = 42;
let blockScopedVariable = 43;
console.log[functionScopedVariable]; // 42
console.log[blockScopedVariable]; // 43
}][];
console.log[functionScopedVariable]; // ReferenceError: functionScopedVariable is not defined
console.log[blockScopedVariable]; // ReferenceError: blockScopedVariable is not defined
1 có cùng phạm vi như [[] => {
var functionScopedVariable = 42;
let blockScopedVariable = 43;
console.log[functionScopedVariable]; // 42
console.log[blockScopedVariable]; // 43
}][];
console.log[functionScopedVariable]; // ReferenceError: functionScopedVariable is not defined
console.log[blockScopedVariable]; // ReferenceError: blockScopedVariable is not defined
4[[] => {
var functionScopedVariable = 42;
let blockScopedVariable = 43;
console.log[functionScopedVariable]; // 42
console.log[blockScopedVariable]; // 43
}][];
console.log[functionScopedVariable]; // ReferenceError: functionScopedVariable is not defined
console.log[blockScopedVariable]; // ReferenceError: blockScopedVariable is not defined
Bên trong một khối
Không thể truy cập các biến được khai báo bằng cách sử dụng
[[] => {
var functionScopedVariable = 42;
let blockScopedVariable = 43;
console.log[functionScopedVariable]; // 42
console.log[blockScopedVariable]; // 43
}][];
console.log[functionScopedVariable]; // ReferenceError: functionScopedVariable is not defined
console.log[blockScopedVariable]; // ReferenceError: blockScopedVariable is not defined
1 bên trong một khối bên ngoài khối đó{
var globalVariable = 42;
let blockScopedVariable = 43;
console.log[globalVariable]; // 42
console.log[blockScopedVariable]; // 43
}
console.log[globalVariable]; // 42
console.log[blockScopedVariable]; // ReferenceError: blockScopedVariable is not defined
Bên trong một vòng lặp
Các biến được khai báo với ______________1 trong các vòng lặp chỉ có thể được tham chiếu bên trong vòng lặp đó
for [var i = 0; i < 3; i++] {
var j = i * 2;
}
console.log[i]; // 3
console.log[j]; // 4
for [let k = 0; k < 3; k++] {
let l = k * 2;
}
console.log[typeof k]; // undefined
console.log[typeof l]; // undefined
// Trying to do console.log[k] or console.log[l] here would throw a ReferenceError.
Vòng lặp có bao đóng
Nếu bạn sử dụng
[[] => {
var functionScopedVariable = 42;
let blockScopedVariable = 43;
console.log[functionScopedVariable]; // 42
console.log[blockScopedVariable]; // 43
}][];
console.log[functionScopedVariable]; // ReferenceError: functionScopedVariable is not defined
console.log[blockScopedVariable]; // ReferenceError: blockScopedVariable is not defined
1 thay vì [[] => {
var functionScopedVariable = 42;
let blockScopedVariable = 43;
console.log[functionScopedVariable]; // 42
console.log[blockScopedVariable]; // 43
}][];
console.log[functionScopedVariable]; // ReferenceError: functionScopedVariable is not defined
console.log[blockScopedVariable]; // ReferenceError: blockScopedVariable is not defined
4 trong một vòng lặp, với mỗi lần lặp, bạn sẽ nhận được một biến mới. Điều đó có nghĩa là bạn có thể sử dụng bao đóng bên trong vòng lặp một cách an toàn// Logs 3 thrice, not what we meant.
for [var i = 0; i < 3; i++] {
setTimeout[[] => console.log[i], 0];
}
// Logs 0, 1 and 2, as expected.
for [let j = 0; j < 3; j++] {
setTimeout[[] => console.log[j], 0];
}
Vùng chết tạm thời
Do vùng chết tạm thời, không thể truy cập các biến được khai báo bằng cách sử dụng
[[] => {
var functionScopedVariable = 42;
let blockScopedVariable = 43;
console.log[functionScopedVariable]; // 42
console.log[blockScopedVariable]; // 43
}][];
console.log[functionScopedVariable]; // ReferenceError: functionScopedVariable is not defined
console.log[blockScopedVariable]; // ReferenceError: blockScopedVariable is not defined
1 trước khi chúng được khai báo. Cố gắng làm như vậy ném một lỗiconsole.log[noTDZ]; // undefined
var noTDZ = 43;
console.log[hasTDZ]; // ReferenceError: hasTDZ is not defined
let hasTDZ = 42;
Không khai báo lại
Bạn không thể khai báo cùng một biến nhiều lần bằng cách sử dụng
[[] => {
var functionScopedVariable = 42;
let blockScopedVariable = 43;
console.log[functionScopedVariable]; // 42
console.log[blockScopedVariable]; // 43
}][];
console.log[functionScopedVariable]; // ReferenceError: functionScopedVariable is not defined
console.log[blockScopedVariable]; // ReferenceError: blockScopedVariable is not defined
1. Bạn cũng không thể khai báo một biến bằng cách sử dụng [[] => {
var functionScopedVariable = 42;
let blockScopedVariable = 43;
console.log[functionScopedVariable]; // 42
console.log[blockScopedVariable]; // 43
}][];
console.log[functionScopedVariable]; // ReferenceError: functionScopedVariable is not defined
console.log[blockScopedVariable]; // ReferenceError: blockScopedVariable is not defined
1 với cùng mã định danh như một biến khác đã được khai báo bằng cách sử dụng [[] => {
var functionScopedVariable = 42;
let blockScopedVariable = 43;
console.log[functionScopedVariable]; // 42
console.log[blockScopedVariable]; // 43
}][];
console.log[functionScopedVariable]; // ReferenceError: functionScopedVariable is not defined
console.log[blockScopedVariable]; // ReferenceError: blockScopedVariable is not defined
4var a;
var a; // Works fine.
let b;
let b; // SyntaxError: Identifier 'b' has already been declared
var c;
let c; // SyntaxError: Identifier 'c' has already been declared
{
var globalVariable = 42;
let blockScopedVariable = 43;
console.log[globalVariable]; // 42
console.log[blockScopedVariable]; // 43
}
console.log[globalVariable]; // 42
console.log[blockScopedVariable]; // ReferenceError: blockScopedVariable is not defined
3 khá giống với [[] => {
var functionScopedVariable = 42;
let blockScopedVariable = 43;
console.log[functionScopedVariable]; // 42
console.log[blockScopedVariable]; // 43
}][];
console.log[functionScopedVariable]; // ReferenceError: functionScopedVariable is not defined
console.log[blockScopedVariable]; // ReferenceError: blockScopedVariable is not defined
1—nó có phạm vi khối và có TDZ. Tuy nhiên, có hai điều khác nhau
Let I trong JavaScript là gì?
let cho phép bạn khai báo các biến bị giới hạn trong phạm vi của câu lệnh khối hoặc biểu thức mà nó được sử dụng , không giống như .
Làm cách nào để khai báo let trong JavaScript?
Trong JavaScript, let là từ khóa được sử dụng để khai báo một biến trong phạm vi khối. .
hàm checkLoopScope[] {
cho i = 4;
for [cho i = 0; i < 10; i++] {
// một số câu lệnh
// x vẫn được biết ở đây và có giá trị
tài liệu. write['Giá trị cuối cùng của x bên ngoài vòng lặp. ' + tôi];
Tôi có nên sử dụng let trong JavaScript không?
Tóm tắt. Về nguyên tắc chung, bạn nên luôn khai báo biến với const, nếu bạn nhận thấy giá trị của biến cần thay đổi, hãy quay lại và thay đổi thành let. Sử dụng let khi bạn biết rằng giá trị của một biến sẽ thay đổi . Sử dụng const cho mọi biến khác.
Var vs let trong JavaScript là gì?
Sự khác biệt giữa let và var nằm ở phạm vi biến mà chúng tạo ra. Các biến được khai báo bởi let chỉ khả dụng bên trong khối mà chúng được xác định. Các biến được khai báo bởi var có sẵn trong hàm mà chúng được khai báo .