Hướng dẫn when to use let and var in javascript? - khi nào sử dụng let và var trong javascript?

Trong JavaScript, cả hai 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();
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 đều được sử dụng để khai báo các biến.

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 sau của JavaScript được gọi là ES6 (ES2015). Và & nbsp; đó là & nbsp; cách ưa thích để khai báo các biến.ES6(ES2015). And it's the preferred way to declare variables.


JavaScript cho vs var

Đâ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.

letvar
Đặt là bao gồm khối.VAR là chức năng phạm vi.
không cho phép các biến tái tạo.VAR cho phép các biến tái tạo.
Tăng cường không xảy ra trong LET.Tăng cường xảy ra trong var.


JavaScript hãy để vs var trong phạm vi địa phương

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

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 cứ đâ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 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. Biến A có thể được sử dụng ở bất cứ đâu bên trong hàm
hello world
Uncaught ReferenceError: b is not defined
1.

Đặt là bao gồm khối

Biến được khai báo 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 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 cứ đâu bên trong hàm (A trở thành chức năng phạm vi).

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ẽ được bao gồm 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, xảy ra lỗi (như được hiển thị ở trên trong chương trình).

Lưu ý: Các biến được khai báo bên trong một hàm sẽ là hàm được giới thiệu 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.
: The variables declared inside a function will be function scoped for both
// 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 and
// 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ác biến

1. Một biến được khai báo 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 xác định lại. Ví dụ,

var a = 5; // 5
var a = 3; // 3

Một biến được khai báo 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 không thể được xác định lại trong cùng một khối hoặc cùng một phạm vi. Ví dụ,

let a = 5;
let a = 3; // error 

Đầu ra

Uncaught SyntaxError: Identifier 'a' has already been declared

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 cứ đâu bên trong hàm (A trở thành chức năng phạm vi).

var a = 5;
console.log(a); // 5
{
    var a = 3;
    console.log(a); // 3
}
console.log(a); // 3

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ẽ được bao gồm khối và chỉ có thể được truy cập bên trong khối
hello world
Uncaught ReferenceError: b is not defined
3.

let a = 5;
console.log(a); // 5
{
    let a = 3;
    console.log(a); // 3
}
console.log(a); // 5

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, xảy ra lỗi (như được hiển thị ở trên trong chương trình).

var a = 2;
for(var a = 0; a < 3; a++) {
    console.log('hello');
}
console.log(a); // 3

Lưu ý: Các biến được khai báo bên trong một hàm sẽ là hàm được giới thiệu 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.3 at the end.

không cho phép các biếnlet is used in a loop, the value of a variable does not change. For example,

let a = 2;
for(let a = 0; a < 3; a++) {
    console.log('hello');
}
console.log(a); // 2

1. Một biến được khai báo 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 xác định lại. Ví dụ,2 at the end.


Một biến được khai báo 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 không thể được xác định lại trong cùng một khối hoặc cùng một phạm vi. Ví dụ,

2. Tái tạo 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 khác hoặc chặn 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();
0

Tái cấu trúc 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 nhau xử lý biến đó như một biến khác nhau. 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();
1

3. Khi một biến được khai báo 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 sử dụng trong một vòng lặp, giá trị của biến đó thay đổi. Ví dụ,


Trong chương trình trên, vòng lặp var a = 5; // 5 var a = 3; // 33 RedClares biến a. Do đó, giá trị của var a = 5; // 5 var a = 3; // 34 được thay đổi thành 3 ở cuối.

Khi một biến được khai báo với 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ụ,

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

var a = 5; // 5
var a = 3; // 3
3 xử lý biến A là một biến khác với biến được khai báo ở trên. Và phạm vi của biến đó chỉ ở bên trong vòng
var a = 5; // 5
var a = 3; // 3
3. Do đó, giá trị của & nbsp; biến A vẫn còn 2 ở cuối.


không cho phép nâng: In case of global scope, both

// 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 and
// 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 will behave in the same way. For example,

// 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();
2

Các biến được khai báo 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 nâng lên đỉnh 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();
3

Các biến được khai báo 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 nâng lên đỉnh phạm vi của chương trình. Ví dụ,

Tôi nên sử dụng LET hay VAR trong JavaScript?

Theo nguyên tắc chung, bạn nên luôn luôn khai báo các biến với Const, nếu bạn nhận ra rằng giá trị của biến cần thay đổi, quay lại và thay đổi nó thành LET. Sử dụng cho 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. Không sử dụng var.Use let when you know that the value of a variable will change. Use const for every other variable. Do not use var.

Khi nào bạn nên sử dụng var qua cho phép?

Hãy thích hợp hơn với VAR vì nó làm giảm phạm vi mà một định danh có thể nhìn thấy.Nó cho phép chúng tôi khai báo một cách an toàn các biến tại trang web sử dụng đầu tiên.const thích hợp hơn để cho.Trừ khi bạn cần phải đột biến một tài liệu tham khảo, hãy sử dụng một tuyên bố const.it reduces the scope in which an identifier is visible. It allows us to safely declare variables at the site of first use. const is preferable to let . Unless you need to mutate a reference, use a const declaration.

Var nào nhanh hơn hay cho phép?

Về mặt so sánh hiệu suất, VAR nhanh hơn và cho phép chậm hơn bên trong các vòng lặp trong khi chạy hoặc thực thi mã.VAR khai báo lại đã khai báo một biến trong cùng một hàm hoặc phạm vi làm tăng lỗi cú pháp trong khi biến được khai báo không thể được xác định lại.var is faster and let is slower inside the loops while running or executing the code. Re-declaring var declared a variable in the same function or scope gives rise to Syntax Error whereas let declared variable cannot be redeclared.

Việc sử dụng Var Let và Const trong JavaScript là gì?

Các biến được khai báo với VAR nằm trong phạm vi hàm.Variabled được khai báo là nằm trong phạm vi khối.Variabled được khai báo là const nằm trong phạm vi khối. Variables declared as let are in the block scope. Variables declared as const are in the block scope.