Var hoặc cho phép vòng lặp javascript

Từ khóa

// program to print the text
// variable a cannot be used here
function greet() {
    let a = 'hello';

    // variable b cannot be used here
    if(a == 'hello'){
        // variable b can be used here
        let b = 'world';
        console.log(a + ' ' + b);
    }

     // variable b cannot be used here
    console.log(a + ' ' + b); // error
}
// variable a cannot be used here

greet();
5 đã được giới thiệu trong phiên bản JavaScript mới hơn được gọi là ES6 (ES2015). Và đó là cách ưa thích để khai báo biến


Dưới đây là tổng quan về sự khác biệt giữa

// program to print the text
// variable a cannot be used here
function greet() {
    let a = 'hello';

    // variable b cannot be used here
    if(a == 'hello'){
        // variable b can be used here
        let b = 'world';
        console.log(a + ' ' + b);
    }

     // variable b cannot be used here
    console.log(a + ' ' + b); // error
}
// variable a cannot be used here

greet();
5 và
// program to print the text
// variable a cannot be used here
function greet() {
    let a = 'hello';

    // variable b cannot be used here
    if(a == 'hello'){
        // variable b can be used here
        let b = 'world';
        console.log(a + ' ' + b);
    }

     // variable b cannot be used here
    console.log(a + ' ' + b); // error
}
// variable a cannot be used here

greet();
4

letvarlet nằm trong phạm vi khối. var là phạm vi chức năng. let không cho phép khai báo lại biến. var cho phép khai báo lại các biến. cẩu không xảy ra trong let. cẩu xảy ra trong var


JavaScript để Vs var trong Phạm vi cục bộ

var là phạm vi chức năng

Biến được khai báo bên trong một hàm với

// program to print the text
// variable a cannot be used here
function greet() {
    let a = 'hello';

    // variable b cannot be used here
    if(a == 'hello'){
        // variable b can be used here
        let b = 'world';
        console.log(a + ' ' + b);
    }

     // variable b cannot be used here
    console.log(a + ' ' + b); // error
}
// variable a cannot be used here

greet();
4 có thể được sử dụng ở bất kỳ đâu trong một hàm. Ví dụ,

// program to print text
// variable a cannot be used here
function greet() {
    // variable a can be used here
    var a = 'hello';
    console.log(a);
}
// variable a cannot be used here

greet(); // hello

Trong chương trình trên, biến a được khai báo bằng

// program to print the text
// variable a cannot be used here
function greet() {
    let a = 'hello';

    // variable b cannot be used here
    if(a == 'hello'){
        // variable b can be used here
        let b = 'world';
        console.log(a + ' ' + b);
    }

     // variable b cannot be used here
    console.log(a + ' ' + b); // error
}
// variable a cannot be used here

greet();
4. Biến a có thể được sử dụng ở bất kỳ đâu bên trong hàm
hello world
Uncaught ReferenceError: b is not defined
1

let là phạm vi khối

Biến được khai báo bằng

// program to print the text
// variable a cannot be used here
function greet() {
    let a = 'hello';

    // variable b cannot be used here
    if(a == 'hello'){
        // variable b can be used here
        let b = 'world';
        console.log(a + ' ' + b);
    }

     // variable b cannot be used here
    console.log(a + ' ' + b); // error
}
// variable a cannot be used here

greet();
5 chỉ có thể được truy cập bên trong một khối mã. Ví dụ,

// program to print the text
// variable a cannot be used here
function greet() {
    let a = 'hello';

    // variable b cannot be used here
    if(a == 'hello'){
        // variable b can be used here
        let b = 'world';
        console.log(a + ' ' + b);
    }

     // variable b cannot be used here
    console.log(a + ' ' + b); // error
}
// variable a cannot be used here

greet();

đầu ra

hello world
Uncaught ReferenceError: b is not defined

Trong chương trình trên, biến a được khai báo bên trong hàm và nó có thể được truy cập ở bất kỳ đâu bên trong hàm (a trở thành phạm vi của hàm)

Tuy nhiên, biến b được khai báo bên trong câu lệnh khối

hello world
Uncaught ReferenceError: b is not defined
3. b sẽ nằm trong phạm vi khối và chỉ có thể được truy cập bên trong khối
hello world
Uncaught ReferenceError: b is not defined
3

Do đó, khi bạn cố gắng truy cập b bên ngoài khối

hello world
Uncaught ReferenceError: b is not defined
3, sẽ xảy ra lỗi (như hình trên trong chương trình)

Ghi chú. Các biến được khai báo bên trong một hàm sẽ nằm trong phạm vi hàm cho cả

// program to print the text
// variable a cannot be used here
function greet() {
    let a = 'hello';

    // variable b cannot be used here
    if(a == 'hello'){
        // variable b can be used here
        let b = 'world';
        console.log(a + ' ' + b);
    }

     // variable b cannot be used here
    console.log(a + ' ' + b); // error
}
// variable a cannot be used here

greet();
4 và
// program to print the text
// variable a cannot be used here
function greet() {
    let a = 'hello';

    // variable b cannot be used here
    if(a == 'hello'){
        // variable b can be used here
        let b = 'world';
        console.log(a + ' ' + b);
    }

     // variable b cannot be used here
    console.log(a + ' ' + b); // error
}
// variable a cannot be used here

greet();
5


let không cho phép khai báo lại Biến

1. Một biến được khai báo bằng

// program to print the text
// variable a cannot be used here
function greet() {
    let a = 'hello';

    // variable b cannot be used here
    if(a == 'hello'){
        // variable b can be used here
        let b = 'world';
        console.log(a + ' ' + b);
    }

     // variable b cannot be used here
    console.log(a + ' ' + b); // error
}
// variable a cannot be used here

greet();
4 có thể được khai báo lại một lần nữa. Ví dụ,

// program to print the text
// variable a cannot be used here
function greet() {
    let a = 'hello';

    // variable b cannot be used here
    if(a == 'hello'){
        // variable b can be used here
        let b = 'world';
        console.log(a + ' ' + b);
    }

     // variable b cannot be used here
    console.log(a + ' ' + b); // error
}
// variable a cannot be used here

greet();
6

Một biến được khai báo bằng

// program to print the text
// variable a cannot be used here
function greet() {
    let a = 'hello';

    // variable b cannot be used here
    if(a == 'hello'){
        // variable b can be used here
        let b = 'world';
        console.log(a + ' ' + b);
    }

     // variable b cannot be used here
    console.log(a + ' ' + b); // error
}
// variable a cannot be used here

greet();
5 không thể được khai báo lại trong cùng một khối hoặc cùng phạm vi. Ví dụ,

// program to print the text
// variable a cannot be used here
function greet() {
    let a = 'hello';

    // variable b cannot be used here
    if(a == 'hello'){
        // variable b can be used here
        let b = 'world';
        console.log(a + ' ' + b);
    }

     // variable b cannot be used here
    console.log(a + ' ' + b); // error
}
// variable a cannot be used here

greet();
8

đầu ra

// program to print the text
// variable a cannot be used here
function greet() {
    let a = 'hello';

    // variable b cannot be used here
    if(a == 'hello'){
        // variable b can be used here
        let b = 'world';
        console.log(a + ' ' + b);
    }

     // variable b cannot be used here
    console.log(a + ' ' + b); // error
}
// variable a cannot be used here

greet();
9

2. Khai báo lại một biến với

// program to print the text
// variable a cannot be used here
function greet() {
    let a = 'hello';

    // variable b cannot be used here
    if(a == 'hello'){
        // variable b can be used here
        let b = 'world';
        console.log(a + ' ' + b);
    }

     // variable b cannot be used here
    console.log(a + ' ' + b); // error
}
// variable a cannot be used here

greet();
4 trong một phạm vi hoặc khối khác cũng thay đổi giá trị của biến bên ngoài. Ví dụ,

// program to print the text
// variable a cannot be used here
function greet() {
    let a = 'hello';

    // variable b cannot be used here
    if(a == 'hello'){
        // variable b can be used here
        let b = 'world';
        console.log(a + ' ' + b);
    }

     // variable b cannot be used here
    console.log(a + ' ' + b); // error
}
// variable a cannot be used here

greet();
1

Khai báo lại một biến với

// program to print the text
// variable a cannot be used here
function greet() {
    let a = 'hello';

    // variable b cannot be used here
    if(a == 'hello'){
        // variable b can be used here
        let b = 'world';
        console.log(a + ' ' + b);
    }

     // variable b cannot be used here
    console.log(a + ' ' + b); // error
}
// variable a cannot be used here

greet();
5 trong một phạm vi hoặc khối khác coi biến đó là một biến khác. Và giá trị của một biến bên ngoài không thay đổi. Ví dụ,

// program to print the text
// variable a cannot be used here
function greet() {
    let a = 'hello';

    // variable b cannot be used here
    if(a == 'hello'){
        // variable b can be used here
        let b = 'world';
        console.log(a + ' ' + b);
    }

     // variable b cannot be used here
    console.log(a + ' ' + b); // error
}
// variable a cannot be used here

greet();
3

3. Khi một biến được khai báo bằng

// program to print the text
// variable a cannot be used here
function greet() {
    let a = 'hello';

    // variable b cannot be used here
    if(a == 'hello'){
        // variable b can be used here
        let b = 'world';
        console.log(a + ' ' + b);
    }

     // variable b cannot be used here
    console.log(a + ' ' + b); // error
}
// variable a cannot be used here

greet();
4 được sử dụng trong một vòng lặp, giá trị của biến đó sẽ thay đổi. Ví dụ,

// program to print the text
// variable a cannot be used here
function greet() {
    let a = 'hello';

    // variable b cannot be used here
    if(a == 'hello'){
        // variable b can be used here
        let b = 'world';
        console.log(a + ' ' + b);
    }

     // variable b cannot be used here
    console.log(a + ' ' + b); // error
}
// variable a cannot be used here

greet();
5

Trong chương trình trên, vòng lặp

// program to print the text
// variable a cannot be used here
function greet() {
    let a = 'hello';

    // variable b cannot be used here
    if(a == 'hello'){
        // variable b can be used here
        let b = 'world';
        console.log(a + ' ' + b);
    }

     // variable b cannot be used here
    console.log(a + ' ' + b); // error
}
// variable a cannot be used here

greet();
63 khai báo lại biến a. Do đó, giá trị của
// program to print the text
// variable a cannot be used here
function greet() {
    let a = 'hello';

    // variable b cannot be used here
    if(a == 'hello'){
        // variable b can be used here
        let b = 'world';
        console.log(a + ' ' + b);
    }

     // variable b cannot be used here
    console.log(a + ' ' + b); // error
}
// variable a cannot be used here

greet();
64 được thay đổi thành 3 ở cuối

Khi một biến được khai báo bằng let được sử dụng trong một vòng lặp, giá trị của một biến không thay đổi. Ví dụ,

// program to print the text
// variable a cannot be used here
function greet() {
    let a = 'hello';

    // variable b cannot be used here
    if(a == 'hello'){
        // variable b can be used here
        let b = 'world';
        console.log(a + ' ' + b);
    }

     // variable b cannot be used here
    console.log(a + ' ' + b); // error
}
// variable a cannot be used here

greet();
8

Trong chương trình trên, vòng lặp

// program to print the text
// variable a cannot be used here
function greet() {
    let a = 'hello';

    // variable b cannot be used here
    if(a == 'hello'){
        // variable b can be used here
        let b = 'world';
        console.log(a + ' ' + b);
    }

     // variable b cannot be used here
    console.log(a + ' ' + b); // error
}
// variable a cannot be used here

greet();
63 coi biến a là một biến khác với biến đã khai báo ở trên. Và phạm vi của biến đó chỉ bên trong vòng lặp
// program to print the text
// variable a cannot be used here
function greet() {
    let a = 'hello';

    // variable b cannot be used here
    if(a == 'hello'){
        // variable b can be used here
        let b = 'world';
        console.log(a + ' ' + b);
    }

     // variable b cannot be used here
    console.log(a + ' ' + b); // error
}
// variable a cannot be used here

greet();
63. Do đó, giá trị của biến a vẫn là 2 ở cuối


let Doesn't Allow Hoisting

Các biến được khai báo bằng

// program to print the text
// variable a cannot be used here
function greet() {
    let a = 'hello';

    // variable b cannot be used here
    if(a == 'hello'){
        // variable b can be used here
        let b = 'world';
        console.log(a + ' ' + b);
    }

     // variable b cannot be used here
    console.log(a + ' ' + b); // error
}
// variable a cannot be used here

greet();
4 được đưa lên đầu phạm vi của chương trình. Ví dụ,

// program to print the text
// variable a cannot be used here
function greet() {
    let a = 'hello';

    // variable b cannot be used here
    if(a == 'hello'){
        // variable b can be used here
        let b = 'world';
        console.log(a + ' ' + b);
    }

     // variable b cannot be used here
    console.log(a + ' ' + b); // error
}
// variable a cannot be used here

greet();
0

Từ khóa

// program to print the text
// variable a cannot be used here
function greet() {
    let a = 'hello';

    // variable b cannot be used here
    if(a == 'hello'){
        // variable b can be used here
        let b = 'world';
        console.log(a + ' ' + b);
    }

     // variable b cannot be used here
    console.log(a + ' ' + b); // error
}
// variable a cannot be used here

greet();
5 không cho phép cẩu. Ví dụ,

// program to print the text
// variable a cannot be used here
function greet() {
    let a = 'hello';

    // variable b cannot be used here
    if(a == 'hello'){
        // variable b can be used here
        let b = 'world';
        console.log(a + ' ' + b);
    }

     // variable b cannot be used here
    console.log(a + ' ' + b); // error
}
// variable a cannot be used here

greet();
1

Nếu bạn muốn tìm hiểu thêm về hoisting, hãy truy cập JavaScript Hoisting


let và var Hỗ trợ trình duyệt

Hầu hết các trình duyệt hiện đại đều hỗ trợ sử dụng

// program to print the text
// variable a cannot be used here
function greet() {
    let a = 'hello';

    // variable b cannot be used here
    if(a == 'hello'){
        // variable b can be used here
        let b = 'world';
        console.log(a + ' ' + b);
    }

     // variable b cannot be used here
    console.log(a + ' ' + b); // error
}
// variable a cannot be used here

greet();
5. Tuy nhiên, một số trình duyệt không hỗ trợ đầy đủ
// program to print the text
// variable a cannot be used here
function greet() {
    let a = 'hello';

    // variable b cannot be used here
    if(a == 'hello'){
        // variable b can be used here
        let b = 'world';
        console.log(a + ' ' + b);
    }

     // variable b cannot be used here
    console.log(a + ' ' + b); // error
}
// variable a cannot be used here

greet();
5

Để tìm hiểu thêm, hãy truy cập JavaScript let browser support


Ghi chú. Trong trường hợp phạm vi toàn cầu, cả

// program to print the text
// variable a cannot be used here
function greet() {
    let a = 'hello';

    // variable b cannot be used here
    if(a == 'hello'){
        // variable b can be used here
        let b = 'world';
        console.log(a + ' ' + b);
    }

     // variable b cannot be used here
    console.log(a + ' ' + b); // error
}
// variable a cannot be used here

greet();
4 và
// program to print the text
// variable a cannot be used here
function greet() {
    let a = 'hello';

    // variable b cannot be used here
    if(a == 'hello'){
        // variable b can be used here
        let b = 'world';
        console.log(a + ' ' + b);
    }

     // variable b cannot be used here
    console.log(a + ' ' + b); // error
}
// variable a cannot be used here

greet();
5 sẽ hoạt động theo cùng một cách. Ví dụ,

Sự khác biệt giữa LET và VAR trong vòng lặp for trong JavaScript là gì?

var và let đều được sử dụng để khai báo biến trong javascript nhưng điểm khác biệt giữa chúng là var là phạm vi chức năng và let là phạm vi khối . Biến được khai báo bằng let không thể được khai báo lại và phải được khai báo trước khi sử dụng trong khi các biến được khai báo bằng từ khóa var được nâng lên.

Chúng ta có thể sử dụng VAR trong vòng lặp for trong JS không?

Sử dụng var x trong vòng lặp for của bạn . Để vượt qua tất cả. nếu vòng lặp for nằm trong phạm vi toàn cầu (i. e. không phải trong một hàm), thì phạm vi cục bộ (phạm vi x được khai báo nếu bạn sử dụng var x ) giống với phạm vi toàn cầu (phạm vi x được khai báo ngầm nếu bạn sử dụng x không có var), vì vậy cả hai .

Sử dụng LET có tốt hơn var JavaScript không?

let có thể được cập nhật nhưng không được khai báo lại. Điều này là do cả hai trường hợp được coi là các biến khác nhau vì chúng có phạm vi khác nhau. Thực tế này khiến let có sự lựa chọn tốt hơn var . Khi sử dụng let , bạn không cần phải bận tâm nếu trước đó bạn đã sử dụng tên cho biến vì biến chỉ tồn tại trong phạm vi của nó.

Tôi có thể sử dụng vòng lặp let bên trong không?

Theo MDN việc sử dụng let trong vòng lặp for như vậy sẽ liên kết biến trong phạm vi phần thân của vòng lặp . Mọi thứ hoạt động như tôi mong đợi khi tôi sử dụng một biến tạm thời bên trong khối.