Files
2nd/10_Wiki/Topic_Programming/Topic_JavaScript/JavaScript_Scope.md
T
Antigravity Agent e9cbf23ab5 docs(10_Wiki): Dev 폴더 누락분 반영 — Topic_Programming으로 통합
이전 재구성 작업에서 Dev/ 폴더가 누락되었던 것을 반영.

- Dev/Topic_Programming(중첩 폴더, 78개)은 Dev 자체 최상위 폴더들
  (Architecture/Conventions/Engineering_Intelligence 등)과 완전 중복이라 제거.
- Dev 최상위 엔지니어링 지식 폴더(Architecture/Conventions/Engineering_Intelligence/
  Failure_Library/Generalized_Principles/Language/Pattern_Catalog/Platform_Guides/
  Subsystems, 77개)는 이미 Topic_Programming/Topic_Programming에 더 최신 버전이
  존재해 중복 제거(고유 콘텐츠 1개는 예외 처리하여 이동 보존).
- Dev의 W3Schools 언어 튜토리얼 폴더(Topic_C/CPP/CSS/CSharp/HOWTO/HTML/Java/
  JavaScript/PHP/Python/SQL/W3CSS, 1201개)는 전부 Topic_Programming 하위로 이동.
- 에이전트 운영 상태(.astra/docs)는 그대로 유지, 콘텐츠 폴더만 정리.
- Topic_Programming 최종 문서 수: 2784 → 3985.
2026-07-05 00:39:13 +09:00

5.9 KiB

id, title, category, status, verification_status, canonical_id, aliases, duplicate_of, source_trust_level, confidence_score, created_at, updated_at, review_reason, merge_history, tags, raw_sources, applied_in, github_commit
id title category status verification_status canonical_id aliases duplicate_of source_trust_level confidence_score created_at updated_at review_reason merge_history tags raw_sources applied_in github_commit
javascript-scope JavaScript Scope Frontend draft conceptual
variable scope
global scope
function scope
block scope
local variables
B 0.88 2026-06-23 2026-06-23
javascript
js
web
frontend
w3schools
scope
variables
let
const
var
https://www.w3schools.com/js/js_scope.asp

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 keywordsvar, 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]

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]

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]

{
  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]

{
  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):

{
  let x = 2;
}
// x can NOT be used here

var ignores block scope:

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

📚 출처 (Sources)

📝 변경 이력 (Change history)

  • 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript Scope" page (Astra wiki-curation, P-Reinforce v3.1 format).