# Hiều về let và const

## 1. Let&#x20;

`let` tạo ra một biến chỉ có thể truy cập được trong block bao quanh nó, khác với `var` - tạo ra một biến có phạm vi truy cập xuyên suốt `function` chứa nó.

Ví dụ:\
Sử dụng `var`: &#x20;

![](https://89779819-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M8jEJk3DAzRpg9xgL2w%2F-M9MsPiAWOT80Xu7llY7%2F-M9MtNp8mtNf1e23vq2W%2FA%CC%89nh%20chu%CC%A3p%20Ma%CC%80n%20hi%CC%80nh%202020-06-09%20lu%CC%81c%2014.04.49.png?alt=media\&token=56ccdb54-98f7-4c87-849b-b23b8a3ae7de)

Sử dụng `let`

![](https://89779819-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M8jEJk3DAzRpg9xgL2w%2F-M9MsPiAWOT80Xu7llY7%2F-M9MtmonLJ1Os1FOKPIJ%2FA%CC%89nh%20chu%CC%A3p%20Ma%CC%80n%20hi%CC%80nh%202020-06-09%20lu%CC%81c%2014.06.37.png?alt=media\&token=d7e011fa-a59d-4ea7-a990-615be5269202)

Ngoài ra, khi ở global scope (tức là không nằm trong một function nào cả), từ khóa `var` tạo ra thuộc tính mới cho global object (`this`), còn `let` thì không:

![](https://89779819-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M8jEJk3DAzRpg9xgL2w%2F-M9MsPiAWOT80Xu7llY7%2F-M9Mtz5i70scGyfwYAEI%2FA%CC%89nh%20chu%CC%A3p%20Ma%CC%80n%20hi%CC%80nh%202020-06-09%20lu%CC%81c%2014.07.20.png?alt=media\&token=282c81e4-3c4a-40cd-8131-79e847b1409a)

## 2. CONST

Const là một biến hằng số, không thể thay đổi giá trị. Nhưng nó chỉ đúng cho những dạng dữ liệu đơn giản như string, số, boolean(primitive type). Còn với object thì không đúng nữa, ta có thể thay đổi thuộc tính của object.

![](https://89779819-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M8jEJk3DAzRpg9xgL2w%2F-M9Mu4dHDe2MrOP-5mzi%2F-M9MuhhAB2O0Tj2iHuM4%2FA%CC%89nh%20chu%CC%A3p%20Ma%CC%80n%20hi%CC%80nh%202020-06-09%20lu%CC%81c%2014.10.39.png?alt=media\&token=7a08fe4a-45ae-4df1-bdcf-5fd1be27716e)

Để tạo một hằng cho một object chúng ta cần sử dụng **Object.freeze** để ngăn chặn việc thay đổi các thuộc tính của object đó.<br>

![](https://89779819-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M8jEJk3DAzRpg9xgL2w%2F-M9Mu4dHDe2MrOP-5mzi%2F-M9MusyH1CmVShsriQsA%2FA%CC%89nh%20chu%CC%A3p%20Ma%CC%80n%20hi%CC%80nh%202020-06-09%20lu%CC%81c%2014.11.17.png?alt=media\&token=2fb431ad-37be-4c01-9605-43fdc1fd8b69)

## 3. Tổng kết :

– Sử dụng **const** như mặc định.\
– Nếu biến cần phải cập nhật giá trị thì dùng **let.**\
– Hạn chế dùng **var**, trừ khi bạn muốn chia sẻ biến đó thông qua nhiều scope khác nhau.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://haonm.gitbook.io/react-the-complete-guide/hieu-ve-let-va-const.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
