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.
Đặ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 defined1.
Đặ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 defined3. 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 defined3.
Do đó, khi bạn cố gắng truy cập B bên ngoài khối
hello world Uncaught ReferenceError: b is not defined3, 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ả
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[];
// 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 defined3. 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 defined3.
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 defined3, 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ụ,
// 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. 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[];
0Tá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[];
13. 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; // 3
3 RedClares biến a. Do đó, giá trị của var a = 5; // 5
var a = 3; // 3
4 được thay đổi thành 3 ở cuối.
var a = 5; // 5
var a = 3; // 3
var a = 5; // 5
var a = 3; // 3
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[];
2Cá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[];
3Cá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ụ,