--- id: javascript-code-blocks title: "JavaScript Code Blocks" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["curly braces", "block statement", "standalone block", "block scope", "statement grouping"] 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", "code-blocks", "scope", "syntax"] raw_sources: ["https://www.w3schools.com/js/js_codeblocks.asp"] applied_in: [] github_commit: "" --- # [[JavaScript Code Blocks]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) A code block is a group of statements wrapped in curly braces `{ }` that are treated as a single unit β€” and with `let`/`const` it also creates a private scope. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Curly braces group statements** β€” a code block is one or more statements enclosed in `{ }`, executed together as a unit. [S1] - **Used by language constructs** β€” functions, `if/else`, `for`, and `while` all use code blocks to hold their body. [S1] - **Blocks define scope** β€” variables declared with `let` and `const` inside a block are block-scoped and accessible only within that block. [S1] - **Standalone blocks exist** β€” a `{ }` block can stand on its own (not attached to a function or control statement) purely to create a local scope for `let`/`const` variables. [S1] - **Benefits of blocks** β€” encapsulation, use of temporary variables, and organized code that avoids name conflicts while staying readable. [S1] ## 🧩 μΆ”μΆœλœ νŒ¨ν„΄ (Extracted patterns) - **Wrap a body in `{ }`** β€” every function/condition/loop body is a code block. [S1] - **Use a standalone block for a temporary scope** β€” `{ let x = ...; }` confines short-lived variables and prevents leaks into the surrounding scope. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) **Curly Braces** JavaScript code blocks are groups of statements enclosed in curly braces `{ }`. They are essential for controlling the flow of execution and for defining variable scope. [S1] **Code Blocks and Statements** A code block lets multiple statements be treated as a single unit. Code blocks are required by functions, `if` statements, and loops. [S1] A function uses a code block: [S1] ```javascript function myFunction() { // This is a code block } ``` An `if...else` statement uses code blocks: [S1] ```javascript if (condition) { // This is a code block } else { // This is a code block } ``` A `for` loop uses a code block: [S1] ```javascript for (expression 1; expression 2; expression 3) { // This is a code block } ``` A `while` loop uses a code block: [S1] ```javascript while (condition) { // This is a code block } ``` **Defining Scope** Variables declared with `let` and `const` inside a code block are block-scoped β€” they are accessible only within that specific block: [S1] ```javascript { let x = 10; // x is accessible here } // x is not accessible here ``` **Standalone Blocks** A code block can exist independently, without being attached to a function or control structure, simply to create a scope for `let` and `const` variables: [S1] ```javascript { let x = 10; let y = 100; let areal = x * y; } ``` Benefits of standalone blocks include encapsulation (variables are confined to the block scope), use of temporary variables, and organized code that prevents name conflicts while maintaining readability. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) The page's snippets are the applied cases: bodies of functions, `if/else`, `for`, and `while`, plus a standalone `{ }` block used to scope temporary variables (`x`, `y`, `areal`). No external project/commit applications found in the source. ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Block-scoped variables (language: JavaScript): ```javascript { let x = 10; // x is accessible here } // x is not accessible here ``` Standalone block as a temporary scope: ```javascript { let x = 10; let y = 100; let areal = x * y; } ``` ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) No contradictions found in the source. ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** 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 var let const]], [[JavaScript Functions]], [[JavaScript If Else]] - **μ°Έμ‘° λ§₯락:** Referenced when explaining how `{ }` both groups statements and confines `let`/`const` variables. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” JavaScript Code Blocks β€” https://www.w3schools.com/js/js_codeblocks.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript Code Blocks" page (Astra wiki-curation, P-Reinforce v3.1 format).