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ếnDướ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[];
4letvarlet 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 có tên là
// 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 defined1
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 defined3. 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 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, 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[];
5let 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[];
6Mộ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[];
92. 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[];
1Khai 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[];
33. 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[];
5Trong 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ốiKhi 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[];
8Trong 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ốilet 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[];
0Từ 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[];
1Nế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
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ụ,