Khi nào nên sử dụng var 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ụ,

Khi nào bạn nên sử dụng var?

Tôi có 3 lý do để bạn sử dụng var trong dự án JavaScript tiếp theo của mình. .
Bạn yêu thích truyền thống và từ chối bất cứ điều gì mới. Var có thể là từ khóa lâu đời nhất để khai báo một biến, nhưng cho đến nay nó là từ khóa duy nhất. .
Bạn thích khả năng thay đổi, ngay cả khi bạn không. .
Bạn thích phạm vi bị rò rỉ. .
Phải có cách tốt hơn

Khi nào bạn sẽ sử dụng var thay vì let?

let cho phép bạn khai báo các biến bị giới hạn trong phạm vi của một câu lệnh khối hoặc biểu thức mà nó được sử dụng, không giống như từ khóa var khai báo một biến trên toàn cục, . .

Tại sao bạn nên luôn luôn sử dụng var?

var yêu cầu gõ ít hơn . Chẳng hạn, nó cũng ngắn hơn và dễ đọc hơn Dictionary