> For the complete documentation index, see [llms.txt](https://jack1in.gitbook.io/font-end/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jack1in.gitbook.io/font-end/2.-javascript/2.1-es6/2.1.1-var-let-const.md).

# 2.1.1 var、let、const

* **var**
* **let**
* **const**

`var` 是屬於 `函式作用域（function scope）`&#x20;

```
function test() {
  var str = 'Hello';
  console.log(str);
}
// Hello
test();

// str is not defined
console.log(str);
```

但是， `var` 因為是 `函式作用域` 的關係，所以在判斷式的區塊不受影響。

```
if (true) {
  var str = 'Hello';
}
// Hello
console.log(str);
```

這樣的用法，很容易造成 `區域變數` 覆蓋 `全域變數` 的問題。

`let` 是屬於 `區域作用域`

`const` 是屬於 `區域作用域`用來定義 `常數（constant）` ，只要宣告完以後就不能再更動了。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://jack1in.gitbook.io/font-end/2.-javascript/2.1-es6/2.1.1-var-let-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.
