--- id: javascript-variables title: "JavaScript Variables" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["JS variables", "var let const", "identifiers", "assignment operator", "data containers"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.87 created_at: 2026-06-23 updated_at: 2026-06-23 review_reason: "" merge_history: [] tags: ["javascript", "js", "web", "frontend", "w3schools", "variables", "var-let-const"] raw_sources: ["https://www.w3schools.com/js/js_variables.asp"] applied_in: [] github_commit: "" --- # [[JavaScript Variables]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) Variables are containers for storing data, declared in four ways (`var`, `let`, `const`, or automatically) β€” and the modern guidance is to prefer `const`, fall back to `let`, and avoid `var`. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Variables are data containers** β€” they store data values. [S1] - **Four ways to declare** β€” automatically, using `var`, using `let`, or using `const`. [S1] - **Identifier rules** β€” names can contain letters, digits, underscores, and dollar signs; must begin with a letter, `$`, or `_`; are case-sensitive; reserved words cannot be used. [S1] - **`$` is treated like a letter** β€” the dollar sign is treated as a letter in identifiers and is often used as an alias for the main function in JavaScript libraries; `_` is sometimes used to denote "private" variables. [S1] - **Assignment vs equality** β€” the `=` operator assigns a value (it does not mean "equal to"). [S1] - **`+` is overloaded** β€” with numbers it adds; with strings it concatenates. [S1] ## 🧩 μΆ”μΆœλœ νŒ¨ν„΄ (Extracted patterns) - **const-first selection** β€” always declare with `const` if the value should not change (including new Arrays, Objects, Functions); use `let` when the value may change; avoid `var`. [S1] - **One statement, many variables** β€” declare several variables in one statement, separated by commas, optionally across multiple lines. [S1] - **String-vs-number `+` evaluation** β€” once a string appears in a `+` chain, subsequent operands are concatenated, so order and types matter. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) **Variables = Data Containers** β€” Variables are containers for storing data (storing data values). [S1] A variable can be declared four ways. Using `let`: [S1] ```javascript let x = 5; let y = 6; let z = x + y; ``` Using `const`: [S1] ```javascript const x = 5; const y = 6; const z = x + y; ``` **JavaScript Identifiers** β€” All JavaScript variables must be identified with unique names. These unique names are called identifiers. The general rules for constructing names for variables (unique identifiers) are: names can contain letters, digits, underscores, and dollar signs; names must begin with a letter, `$`, or `_`; names are case-sensitive (`y` and `Y` are different variables); and reserved words (like JavaScript keywords) cannot be used as names. [S1] **JavaScript Underscore (`_`)** β€” Since JavaScript treats the underscore as a letter, identifiers containing `_` are valid variable names. Some programmers like to use underscores to denote "private (hidden)" variables. [S1] **JavaScript Dollar Sign (`$`)** β€” Since JavaScript treats the dollar sign as a letter, identifiers containing `$` are valid variable names. Using the dollar sign is not very common in JavaScript, but professional programmers often use it as an alias for the main function in a JavaScript library. [S1] **When to Use var, let, or const?** β€” The source's guidance: always declare variables; always use `const` if the value should not be changed; always use `const` if the type should not be changed (Arrays and Objects); only use `let` if you can't use `const`; only use `var` if you MUST support old browsers. [S1] **One Statement, Many Variables** β€” You can declare many variables in one statement. Start the statement with `let` and separate the variables by comma: [S1] ```javascript let person = "John Doe", carName = "Volvo", price = 200; ``` A declaration can span multiple lines: [S1] ```javascript let person = "John Doe", carName = "Volvo", price = 200; ``` **The Assignment Operator** β€” In JavaScript, the `=` sign is an "assignment" operator, not an "equal to" operator. [S1] **JavaScript Arithmetic** β€” You can do arithmetic with JavaScript variables, using operators like `=` and `+`. The value of `x` differs depending on whether operands are numbers or strings: [S1] ```javascript let x = 5 + 2 + 3; ``` ```javascript let x = "John" + " " + "Doe"; ``` If you put a number in quotes, the rest of the numbers will be treated as strings and concatenated: [S1] ```javascript let x = "5" + 2 + 3; ``` ```javascript let x = 2 + 3 + "5"; ``` ## πŸ› οΈ 적용 사둀 (Applied in summary) The page's own snippets are the canonical applied examples β€” `let`/`const` declarations computing `x + y`, the comma-separated multi-variable statement, and the `+` arithmetic vs concatenation cases. No external project/commit applications found in the source. ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Declare and compute: ```javascript let x = 5; let y = 6; let z = x + y; ``` Many variables, one statement: ```javascript let person = "John Doe", carName = "Volvo", price = 200; ``` ## βš–οΈ 비ꡐ 및 선택 κΈ°μ€€ (Comparison & decision criteria) The source gives explicit selection rules among the declaration keywords: [S1] | Keyword | Use when | |---------|----------| | `const` | The value (or type, for Arrays/Objects) should not change β€” the default choice | | `let` | You cannot use `const` because the value will change | | `var` | Only if you MUST support old browsers | ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) No contradictions found in the source. (The page notes `var` was the only option before 2015 (ES6) but is now discouraged β€” an update in guidance, not a contradiction.) ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual (μ‹€μ œ 적용 사둀 발견 μ‹œ applied/validated둜 승격 κ°€λŠ₯) - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.87 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[JavaScript Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[JavaScript Let]], [[JavaScript Const]], [[JavaScript Syntax]] - **μ°Έμ‘° λ§₯락:** The foundation for all data handling; refined by the dedicated Let and Const pages. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” JavaScript Variables β€” https://www.w3schools.com/js/js_variables.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript Variables" page (Astra wiki-curation, P-Reinforce v3.1 format).