--- id: javascript-var-let-const title: "JavaScript var let const" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["var let const", "variable declarations", "block scope", "redeclaration", "const reassignment"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.86 created_at: 2026-06-23 updated_at: 2026-06-23 review_reason: "" merge_history: [] tags: ["javascript", "js", "web", "frontend", "w3schools", "var", "let", "const", "scope", "variables"] raw_sources: ["https://www.w3schools.com/js/js_varletconst.asp"] applied_in: [] github_commit: "" --- # [[JavaScript var let const]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) `var`, `let`, and `const` differ in scope, redeclaration, and reassignment β€” modern best practice is `const` by default, `let` when you must reassign, and avoid `var`. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Three declaration keywords** β€” `var`, `let`, and `const` declare variables but with different scoping and mutation rules. [S1] - **Scope** β€” `var` has function or global scope; `let` and `const` are confined to the nearest pair of curly braces `{ }` (block scope). [S1] - **Reassignment** β€” `var` and `let` can be updated (reassigned); `const` cannot be updated with a new value. [S1] - **Redeclaration** β€” `var` can be redeclared; `let` and `const` cannot be redeclared in the same scope. [S1] - **const is "const reference," not "const value"** β€” you cannot reassign a `const` variable to a completely new value, but you can still change the properties of an object or array assigned to it. [S1] - **Hoisting** β€” `var` is hoisted and initialized to `undefined`; `let` and `const` are also hoisted but *not* initialized, creating a temporal dead zone where access throws a `ReferenceError`. [S1] ## 🧩 μΆ”μΆœλœ νŒ¨ν„΄ (Extracted patterns) - **`const` by default** β€” use `const` unless you know you must reassign. [S1] - **`let` only when reassigning** β€” switch to `let` only when the variable's value must change after initialization. [S1] - **Avoid `var`** β€” its function-scope and redeclaration rules cause bugs; modern code avoids it. [S1] - **Mutate, don't reassign, a `const` object/array** β€” change properties/elements in place rather than rebinding the variable. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) **The three keywords** JavaScript declares variables with `var`, `let`, or `const`. They differ in scope, in whether they can be redeclared, and in whether they can be reassigned. [S1] **Comparison of scope, reassignment, and redeclaration** [S1] | Keyword | Scope | Reassignment | Redeclaration | |---------|-------|--------------|---------------| | `var` | Function or global scope | Can be updated | Can be redeclared | | `let` | Block scope `{ }` | Can be updated | Cannot be redeclared | | `const` | Block scope `{ }` | Cannot be updated | Cannot be redeclared | **Block scope (`let`)** Both `let` and `const` are confined strictly to the nearest pair of curly braces `{ }`. A variable declared with `let` inside a block is not accessible outside it: [S1] ```javascript { let x = 10; // x is accessible here } // x is NOT accessible here ``` **`var` can be redeclared** A variable declared with `var` can be redeclared in the same scope without error. (`let` cannot be redeclared in the same scope.) [S1] **`const` cannot be reassigned, but objects/arrays can be mutated** You cannot reassign a `const` variable with a completely new value, but you can still change the properties of an object β€” or the elements of an array β€” declared with `const`. [S1] > Note (W3Schools): On this page these behaviors are demonstrated through "Try it Yourself" examples (linked to `tryit.asp`) and the comparison table above. The exact full source of the individual "Try it Yourself" boxes is not rendered inline on the page β€” *Not found in source* (verbatim per-example code beyond the snippets shown here). [S1] **Hoisting** Variables declared with `var` are hoisted to the top of their scope and initialized as `undefined`, so they can be referenced before their declaration line. `let` and `const` are also hoisted but *not* initialized, creating a Temporal Dead Zone where accessing them before declaration causes a `ReferenceError`. [S1] **Recommendation** Use `const` by default, and only use `let` when you have to reassign the variable after initialization. Avoid using `var`. Modern JavaScript standards recommend avoiding `var` to minimize bugs related to its scoping and redeclaration rules. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) The page applies these rules to everyday declarations: choosing `const` for values that never rebind, `let` for counters/accumulators that change, and avoiding `var` entirely. No external project/commit applications found in the source. ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Block scope of `let` (language: JavaScript): ```javascript { let x = 10; } // x is NOT accessible here ``` ## βš–οΈ 비ꡐ 및 선택 κΈ°μ€€ (Comparison & decision criteria) - **Scope:** `var` β†’ function/global; `let`/`const` β†’ block `{ }`. Prefer block scope to limit visibility. [S1] - **Reassignment:** `var`/`let` can be updated; `const` cannot. Use `let` only when reassignment is required. [S1] - **Redeclaration:** `var` can be redeclared; `let`/`const` cannot β€” making accidental redeclaration an early error. [S1] - **Overall default:** `const` first, `let` when you must reassign, avoid `var`. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) No contradictions found in the source. The page notes that `let` and `const` (block scope) are ES6 (2015) features, superseding the older `var`-only model. ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual (μ‹€μ œ 적용 사둀 발견 μ‹œ applied/validated둜 승격 κ°€λŠ₯) - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.86 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[JavaScript Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[JavaScript Scope]], [[JavaScript Hoisting]], [[JavaScript Code Blocks]], [[JavaScript Const]] - **μ°Έμ‘° λ§₯락:** The reference for choosing a declaration keyword in any JavaScript code. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” JavaScript var let const β€” https://www.w3schools.com/js/js_varletconst.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript var let const" page (Astra wiki-curation, P-Reinforce v3.1 format).