--- 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).