Câu hỏi
Sự khác biệt của biến dùng var
, let
và const
Sự khác biệt của biến dùng var
, let
và const
Từ phiên bản ES6, từ khoá let và const được thêm vào cho khai báo biến.
keyword | const | let | var |
---|---|---|---|
global scope | no | no | yes |
function scope | yes | yes | yes |
block scope | yes | yes | no |
can be reassigned | no | yes | yes |
Ta thử xem các ví dụ:
```js var variable1 = 23;
let variable2 = 89;
function catchValues(){ console.log(variable1); console.log(variable2);
// Both the variables can be accessed anywhere since they are declared in the global scope }
window.variable1; // Returns the value 23
window.variable2; // Re...