--- id: javascript-array-const title: "JavaScript Array Const" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["const array", "constant array", "const block scope", "const reassignment", "array const declaration"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.89 created_at: 2026-06-23 updated_at: 2026-06-23 review_reason: "" merge_history: [] tags: ["javascript", "js", "web", "frontend", "w3schools", "array", "const"] raw_sources: ["https://www.w3schools.com/js/js_array_const.asp"] applied_in: [] github_commit: "" --- # [[JavaScript Array Const]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) A `const` array cannot be *reassigned*, but its *elements* can be changed, added, or removed β€” `const` protects the binding, not the contents. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **`const` is common practice for arrays** β€” ES2015 (ES6) made declaring arrays with `const` standard practice. [S1] - **A const array cannot be reassigned** β€” assigning a new array to the same `const` name is an ERROR. [S1] - **Const array elements CAN change** β€” you can change, add (`push`), or remove elements of a `const` array. [S1] - **Const must be assigned when declared** β€” declaring without assignment is a syntax error. [S1] - **Const has block scope** β€” a `const` declared inside a block is not the same variable as one outside the block. [S1] - **Const cannot be redeclared** β€” you cannot redeclare or reassign a `const` in the same scope, but you can declare a new `const` with the same name in a different block scope. [S1] ## 🧩 μΆ”μΆœλœ νŒ¨ν„΄ (Extracted patterns) - **Mutate-not-rebind pattern** β€” use `const` for arrays you will mutate in place (`cars[0] = ...`, `cars.push(...)`) but never reassign. [S1] - **Block-scoped shadowing** β€” declare a same-named `const` in a nested block to safely shadow the outer one. [S1] - **Declare-and-initialize** β€” always assign a `const` at declaration; deferred assignment is illegal. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) **Const arrays** β€” It is a common practice to declare arrays using `const`: [S1] ```javascript const cars = ["Saab", "Volvo", "BMW"]; ``` **Cannot be Reassigned** β€” an array declared with `const` cannot be reassigned: [S1] ```javascript const cars = ["Saab", "Volvo", "BMW"]; cars = ["Toyota", "Volvo", "Audi"]; // ERROR ``` **Elements Can be Reassigned** β€” 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"); ``` **Assigned When Declared** β€” JavaScript `const` variables must be assigned a value when they are declared. This means an array declared with `const` must be initialized when it is declared. Declaring without initializing is a syntax error: [S1] ```javascript const cars; cars = ["Saab", "Volvo", "BMW"]; ``` This is not how `var` works. With `var`, you can use the variable before it is declared: [S1] ```javascript cars = ["Saab", "Volvo", "BMW"]; var cars; ``` **Const Block Scope** β€” a variable declared with `const` has block scope. A `const` declared in a block `{}` is not the same as a variable declared outside the block: [S1] ```javascript const cars = ["Saab", "Volvo", "BMW"]; // Here cars[0] is "Saab" { const cars = ["Toyota", "Volvo", "BMW"]; // Here cars[0] is "Toyota" } // Here cars[0] is "Saab" ``` Redeclaring a `var` array with `var`, in another block, in the same program, is allowed and behaves differently (no block scope): [S1] ```javascript var cars = ["Saab", "Volvo", "BMW"]; // Here cars[0] is "Saab" { var cars = ["Toyota", "Volvo", "BMW"]; // Here cars[0] is "Toyota" } // Here cars[0] is "Toyota" ``` **Redeclaring Arrays** β€” Redeclaring a `var` array is allowed anywhere in a program: [S1] ```javascript var cars = ["Volvo", "BMW"]; // Allowed var cars = ["Toyota", "BMW"]; // Allowed cars = ["Volvo", "Saab"]; // Allowed ``` Redeclaring or reassigning an existing `var` or `const` array to `const`, in the same scope or block, is not allowed: [S1] ```javascript var cars = ["Volvo", "BMW"]; // Allowed const cars = ["Volvo", "BMW"]; // Not allowed { var cars = ["Volvo", "BMW"]; // Allowed const cars = ["Volvo", "BMW"]; // Not allowed } ``` Redeclaring or reassigning an existing `const` array, in the same scope or block, is not allowed: [S1] ```javascript const cars = ["Volvo", "BMW"]; // Allowed const cars = ["Volvo", "BMW"]; // Not allowed var cars = ["Volvo", "BMW"]; // Not allowed cars = ["Volvo", "BMW"]; // Not allowed { const cars = ["Volvo", "BMW"]; // Allowed const cars = ["Volvo", "BMW"]; // Not allowed var cars = ["Volvo", "BMW"]; // Not allowed cars = ["Volvo", "BMW"]; // Not allowed } ``` Redeclaring a `const` array, in another scope or block, is allowed: [S1] ```javascript const cars = ["Volvo", "BMW"]; // Allowed { const cars = ["Volvo", "BMW"]; // Allowed } { const cars = ["Volvo", "BMW"]; // Allowed } ``` ## πŸ› οΈ 적용 사둀 (Applied in summary) The page's own snippets are the canonical applied examples β€” declaring a `cars` array with `const`, mutating its elements via index assignment and `push`, and demonstrating block-scope shadowing. No external project/commit applications found in the source. ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Declare and mutate (allowed): ```javascript const cars = ["Saab", "Volvo", "BMW"]; cars[0] = "Toyota"; cars.push("Audi"); ``` Reassign (error): ```javascript const cars = ["Saab", "Volvo", "BMW"]; cars = ["Toyota", "Volvo", "Audi"]; // ERROR ``` ## βš–οΈ 비ꡐ 및 선택 κΈ°μ€€ (Comparison & decision criteria) - **`const` vs `var` for arrays** β€” `const` has block scope, must be initialized at declaration, and cannot be reassigned or redeclared in the same scope; `var` lacks block scope, can be used before declaration, and can be freely redeclared. `const` is the recommended common practice. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) No contradictions found in the source. The source notes that declaring arrays with `const` became common practice with ES2015 (ES6). [S1] ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual (μ‹€μ œ 적용 사둀 발견 μ‹œ applied/validated둜 승격 κ°€λŠ₯) - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.89 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[JavaScript Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[JavaScript Arrays]], [[JavaScript Array Iteration]], [[JavaScript Const]] - **μ°Έμ‘° λ§₯락:** Referenced when deciding how to declare arrays and what mutability `const` guarantees. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” JavaScript Array Const β€” https://www.w3schools.com/js/js_array_const.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript Array Const" page (Astra wiki-curation, P-Reinforce v3.1 format).