docs(10_Wiki): W3Schools 위키화 — HTML/CSS/JavaScript(core)
W3Schools 튜토리얼을 P-Reinforce v3.1 포맷으로 위키화(영어 본문, 한/영 섹션 헤더). - Topic_HTML: 59문서 (튜토리얼+예제, 레퍼런스/메타 제외) - Topic_CSS: 190문서 (메인 + Advanced/Flexbox/Grid/RWD 전체) - Topic_JavaScript: 120문서 (코어 언어; Temporal/DOM상세/BOM/WebAPI/AJAX/jQuery/Graphics 등은 후속) 각 폴더 00_INDEX.md(MOC) 포함. 코드 verbatim, 미확인분은 "Not found in source" 표기. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
---
|
||||
id: javascript-scope
|
||||
title: "JavaScript Scope"
|
||||
category: "Frontend"
|
||||
status: "draft"
|
||||
verification_status: "conceptual"
|
||||
canonical_id: ""
|
||||
aliases: ["variable scope", "global scope", "function scope", "block scope", "local variables"]
|
||||
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", "scope", "variables", "let", "const", "var"]
|
||||
raw_sources: ["https://www.w3schools.com/js/js_scope.asp"]
|
||||
applied_in: []
|
||||
github_commit: ""
|
||||
---
|
||||
|
||||
# [[JavaScript Scope]]
|
||||
|
||||
## 🎯 한 줄 통찰 (One-line insight)
|
||||
Scope determines the accessibility (visibility) of variables — JavaScript has three kinds: global, function, and block. [S1]
|
||||
|
||||
## 🧠 핵심 개념 (Core concepts)
|
||||
- **Scope = accessibility** — scope determines where variables are visible and reachable in your code. [S1]
|
||||
- **Three scopes** — JavaScript has global scope, function scope, and (since ES6) block scope. [S1]
|
||||
- **`let` and `const` are block scoped** — variables declared with `let` and `const` inside a block `{ }` cannot be accessed outside it; `var` is *not* block scoped. [S1]
|
||||
- **Function scope applies to all three keywords** — `var`, `let`, and `const` are all function-scoped when declared inside a function and cannot be accessed from outside. [S1]
|
||||
- **Automatic globals are a trap** — assigning a value to an undeclared variable makes it a global automatically, which is discouraged; strict mode prevents it. [S1]
|
||||
- **Lifetime** — local (function) variables live only while the function runs; global variables live until the page/browser closes. [S1]
|
||||
|
||||
## 🧩 추출된 패턴 (Extracted patterns)
|
||||
- **Declare in the narrowest scope** — keep variables local to the function or block that uses them. [S1]
|
||||
- **Prefer block scope for safety** — use `let`/`const` inside blocks to confine variables and avoid leaks. [S1]
|
||||
- **Avoid undeclared assignment** — always declare before assigning to avoid creating accidental globals. [S1]
|
||||
|
||||
## 📖 세부 내용 (Details)
|
||||
**What is scope?**
|
||||
Scope determines the accessibility (visibility) of variables. JavaScript has 3 types of scope: Block scope, Function scope, and Global scope. [S1]
|
||||
|
||||
**Global Scope**
|
||||
Variables declared *outside* any function or block have global scope and can be accessed everywhere, whether declared with `var`, `let`, or `const`: [S1]
|
||||
```javascript
|
||||
let carName = "Volvo";
|
||||
// code here can use carName
|
||||
|
||||
function myFunction() {
|
||||
// code here can also use carName
|
||||
}
|
||||
```
|
||||
|
||||
**Function Scope (Local Scope)**
|
||||
Variables declared inside a function are local to that function and cannot be accessed from outside. Each function creates a new scope, and all three declaration keywords behave the same way here: [S1]
|
||||
```javascript
|
||||
function myFunction1() {
|
||||
var carName = "Volvo"; // Function Scope
|
||||
}
|
||||
|
||||
function myFunction2() {
|
||||
let carName = "Volvo"; // Function Scope
|
||||
}
|
||||
|
||||
function myFunction3() {
|
||||
const carName = "Volvo"; // Function Scope
|
||||
}
|
||||
```
|
||||
|
||||
**Block Scope**
|
||||
Before ES6 (2015), JavaScript had only global scope and function scope. ES6 introduced `let` and `const`, which provide block scope. Variables declared inside a `{ }` block with `let` or `const` cannot be accessed from outside the block: [S1]
|
||||
```javascript
|
||||
{
|
||||
let x = 2;
|
||||
}
|
||||
// x can NOT be used here
|
||||
```
|
||||
Variables declared with `var` do *not* have block scope and *can* be accessed outside the block: [S1]
|
||||
```javascript
|
||||
{
|
||||
var x = 2;
|
||||
}
|
||||
// x CAN be used here
|
||||
```
|
||||
|
||||
**Automatically Global**
|
||||
If you assign a value to a variable that has not been declared, it automatically becomes a global variable. This is discouraged. [S1]
|
||||
|
||||
**Strict Mode**
|
||||
In "strict mode", undeclared variables are not automatically global. [S1]
|
||||
|
||||
**The Lifetime of JavaScript Variables**
|
||||
The lifetime of a JavaScript variable starts when it is declared. Function (local) variables are deleted when the function completes. Global variables are deleted when you close the page (or browser window). [S1]
|
||||
|
||||
## 🛠️ 적용 사례 (Applied in summary)
|
||||
The page's own snippets illustrate the rule directly: the same `carName` variable visible globally vs. confined inside `myFunction1/2/3`, and the contrast between `let x` (block scoped) and `var x` (leaks out of the block). No external project/commit applications found in the source.
|
||||
|
||||
## 💻 코드 패턴 (Code patterns)
|
||||
Block scope with `let`/`const` (language: JavaScript):
|
||||
```javascript
|
||||
{
|
||||
let x = 2;
|
||||
}
|
||||
// x can NOT be used here
|
||||
```
|
||||
`var` ignores block scope:
|
||||
```javascript
|
||||
{
|
||||
var x = 2;
|
||||
}
|
||||
// x CAN be used here
|
||||
```
|
||||
|
||||
## ⚖️ 비교 및 선택 기준 (Comparison & decision criteria)
|
||||
- **`var` vs. `let`/`const` for block scope** — only `let` and `const` are confined to the block; `var` escapes to the enclosing function/global scope. Choose `let`/`const` to limit visibility. [S1]
|
||||
- **Global vs. local** — declare globally only when the value must be shared everywhere; otherwise keep variables local for shorter lifetime and fewer collisions. [S1]
|
||||
|
||||
## ⚖️ 모순 및 업데이트 (Contradictions & updates)
|
||||
No contradictions found in the source.
|
||||
|
||||
## ✅ 검증 상태 및 신뢰도
|
||||
- **상태:** draft
|
||||
- **검증 단계:** conceptual (실제 적용 사례 발견 시 applied/validated로 승격 가능)
|
||||
- **출처 신뢰도:** B (W3Schools — widely used educational reference, not a primary standards body)
|
||||
- **신뢰 점수:** 0.88
|
||||
- **중복 검사 결과:** 신규 생성 (New discovery)
|
||||
|
||||
## 🔗 지식 그래프 (Knowledge Graph)
|
||||
- **상위/루트:** [[JavaScript Tutorial]]
|
||||
- **관련 개념:** [[JavaScript var let const]], [[JavaScript Code Blocks]], [[JavaScript Hoisting]], [[JavaScript Strict Mode]]
|
||||
- **참조 맥락:** Underpins decisions about where to declare variables and which keyword to use.
|
||||
|
||||
## 📚 출처 (Sources)
|
||||
- [S1] W3Schools — JavaScript Scope — https://www.w3schools.com/js/js_scope.asp
|
||||
|
||||
## 📝 변경 이력 (Change history)
|
||||
- 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript Scope" page (Astra wiki-curation, P-Reinforce v3.1 format).
|
||||
Reference in New Issue
Block a user