Files
2nd/10_Wiki/Topic_JavaScript/JavaScript_Const.md
T
koriweb 9609c04755 docs(10_Wiki): W3Schools 위키화 — HTML/CSS/JavaScript(core)
W3Schools 튜토리얼을 P-Reinforce v3.1 포맷으로 위키화(영어 본문, 한/영 섹션 헤더).
- Topic_HTML: 59문서 (튜토리얼+예제, 레퍼런스/메타 제외)
- Topic_CSS: 190문서 (메인 + Advanced/Flexbox/Grid/RWD 전체)
- Topic_JavaScript: 120문서 (코어 언어; Temporal/DOM상세/BOM/WebAPI/AJAX/jQuery/Graphics 등은 후속)
각 폴더 00_INDEX.md(MOC) 포함. 코드 verbatim, 미확인분은 "Not found in source" 표기.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 19:21:18 +09:00

195 lines
7.5 KiB
Markdown

---
id: javascript-const
title: "JavaScript Const"
category: "Frontend"
status: "draft"
verification_status: "conceptual"
canonical_id: ""
aliases: ["const keyword", "constant reference", "constant objects", "constant arrays", "const hoisting"]
duplicate_of: ""
source_trust_level: "B"
confidence_score: 0.88
created_at: 2026-06-23
updated_at: 2026-06-23
review_reason: ""
merge_history: []
tags: ["javascript", "js", "web", "frontend", "w3schools", "const", "block-scope"]
raw_sources: ["https://www.w3schools.com/js/js_const.asp"]
applied_in: []
github_commit: ""
---
# [[JavaScript Const]]
## 🎯 한 줄 통찰 (One-line insight)
`const` (ES6, 2015) declares a block-scoped constant reference that cannot be reassigned or redeclared — but for objects and arrays it only fixes the reference, so their contents can still be changed. [S1]
## 🧠 핵심 개념 (Core concepts)
- **Cannot be reassigned** — a variable declared with `const` cannot be reassigned. [S1]
- **Must be assigned at declaration** — `const` variables must be assigned a value when they are declared. [S1]
- **Constant reference, not constant value** — `const` defines a constant reference to a value; it does not make the value itself immutable. [S1]
- **Objects/arrays are mutable** — you cannot reassign a `const` array or object, but you can change array elements and object properties. [S1]
- **Block scope** — `const` (like `let`) has block scope. [S1]
- **Hoisted but not initialized** — `const` is hoisted but not initialized; using it before declaration causes a `ReferenceError`. [S1]
## 🧩 추출된 패턴 (Extracted patterns)
- **const-by-default** — declare with `const` whenever the value should not change; reserve `let` for the cases where it must. [S1]
- **Mutate-not-reassign** — for collections, modify elements/properties in place rather than rebinding the variable. [S1]
- **Declare-with-value** — `const` cannot be split into a bare declaration plus a later assignment; assign at the point of declaration. [S1]
## 📖 세부 내용 (Details)
**Cannot be Reassigned** — A variable declared with `const` cannot be reassigned: [S1]
```javascript
const PI = 3.141592653589793;
PI = 3.14; // This will give an error
PI = PI + 10; // This will also give an error
```
**Must be Assigned** — JavaScript `const` variables must be assigned a value when they are declared. Correct: [S1]
```javascript
const PI = 3.14159265359;
```
Incorrect: [S1]
```javascript
const PI;
PI = 3.14159265359;
```
**When to use JavaScript const?** — Always declare a variable with `const` when you know that the value should not be changed. Use `const` when you declare: a new Array, a new Object, a new Function, or a new RegExp. [S1]
**Constant Objects and Arrays** — The keyword `const` is a little misleading. It does not define a constant value; it defines a constant reference to a value. Because of this you can NOT reassign a constant value, array, or object, but you CAN change the elements of a constant array and change the properties of a constant object. [S1]
**Constant Arrays** — You can change the elements of a constant array: [S1]
```javascript
// You can create a constant array:
const cars = ["Saab", "Volvo", "BMW"];
// You can change an element:
cars[0] = "Toyota";
// You can add an element:
cars.push("Audi");
```
But you can NOT reassign the array: [S1]
```javascript
const cars = ["Saab", "Volvo", "BMW"];
cars = ["Toyota", "Volvo", "Audi"]; // ERROR
```
**Constant Objects** — You can change the properties of a constant object: [S1]
```javascript
// You can create a const object:
const car = {type:"Fiat", model:"500", color:"white"};
// You can change a property:
car.color = "red";
// You can add a property:
car.owner = "Johnson";
```
But you can NOT reassign the object: [S1]
```javascript
const car = {type:"Fiat", model:"500", color:"white"};
car = {type:"Volvo", model:"EX60", color:"red"} // ERROR
```
**Block Scope** — Declaring a variable with `const` is similar to `let` regarding Block Scope. The `x` declared in the block is not the same as the `x` declared outside the block: [S1]
```javascript
const x = 10;
// Here x is 10
{
const x = 2;
// Here x is 2
}
// Here x is 10
```
**Redeclaring** — Redeclaring an existing `var` or `let` variable to `const`, in the same scope, is not allowed: [S1]
```javascript
var x = 2; // Allowed
const x = 2; // Not allowed
{
let x = 2; // Allowed
const x = 2; // Not allowed
}
```
Reassigning or redeclaring an existing `const`, in the same scope, is not allowed: [S1]
```javascript
const x = 2; // Allowed
x = 2; // Not allowed
var x = 2; // Not allowed
let x = 2; // Not allowed
const x = 2; // Not allowed
```
Redeclaring a `const`, in another scope or in another block, is allowed: [S1]
```javascript
const x = 2; // Allowed
{
const x = 3; // Allowed
}
{
const x = 4; // Allowed
}
```
**Hoisting** — Variables defined with `const` are hoisted to the top, but not initialized. Using a `const` variable before it is declared results in a `ReferenceError`: [S1]
```javascript
alert (carName);
const carName = "Volvo";
```
## 🛠️ 적용 사례 (Applied in summary)
The page's own snippets are the canonical applied examples — the reassignment errors on `PI`, mutating vs reassigning a `const` array and object, block-scoped `const x`, the redeclaration rules, and the hoisting `ReferenceError`. No external project/commit applications found in the source.
## 💻 코드 패턴 (Code patterns)
Constant reference:
```javascript
const PI = 3.141592653589793;
```
Mutate a const collection without reassigning:
```javascript
const cars = ["Saab", "Volvo", "BMW"];
cars[0] = "Toyota";
cars.push("Audi");
```
## ⚖️ 비교 및 선택 기준 (Comparison & decision criteria)
The source compares the three declaration keywords: [S1]
| Feature | var | let | const |
|---------|-----|-----|-------|
| Scope | Function or global scope | Block-scope `{ }` | Block-scope `{ }` |
| Reassignment | Can be updated | Can be updated | Cannot be updated |
| Redeclaration | Can be redeclared | Cannot be redeclared | Cannot be redeclared |
| Hoisting | Initialized as `undefined` | Hoisted, not initialized | Hoisted, not initialized |
Choose `const` whenever the binding should never be reassigned (the default for Arrays, Objects, Functions, RegExp); use `let` only when reassignment is required; avoid `var`. [S1]
## ⚖️ 모순 및 업데이트 (Contradictions & updates)
No contradictions found in the source. The source highlights that "const" is itself a little misleading — it fixes the reference, not the value — which is a clarification rather than a contradiction.
## ✅ 검증 상태 및 신뢰도
- **상태:** draft
- **검증 단계:** conceptual (실제 적용 사례 발견 시 applied/validated로 승격 가능)
- **출처 신뢰도:** B (W3Schools — widely used educational reference, not a primary standards body)
- **신뢰 점수:** 0.88
- **중복 검사 결과:** 신규 생성 (New discovery)
## 🔗 지식 그래프 (Knowledge Graph)
- **상위/루트:** [[JavaScript Tutorial]]
- **관련 개념:** [[JavaScript Variables]], [[JavaScript Let]], [[JavaScript Statements]]
- **참조 맥락:** The immutable-binding declaration keyword, the recommended default, paired with `let`.
## 📚 출처 (Sources)
- [S1] W3Schools — JavaScript Const — https://www.w3schools.com/js/js_const.asp
## 📝 변경 이력 (Change history)
- 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript Const" page (Astra wiki-curation, P-Reinforce v3.1 format).