Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Windows Tipps & SecurityGarmin Cirqa im Test: Fitness-Tracker ohne Display(22.09.2026 um 10:30 Uhr)
Windows Tipps & SecuritySicher online bezahlen: 7 Methoden im Praxis-Check(22.09.2026 um 09:00 Uhr)
Sichere Programmierunghas_tokens: true is a boolean. 476 of 791 have no market behind them.(22.09.2026 um 10:00 Uhr)
Sichere ProgrammierungClaude Code Hooks – Safety Through Invariants(22.09.2026 um 10:04 Uhr)
Sichere ProgrammierungYour MCP Tool Just Returned a Secret. Did It Need To?(22.09.2026 um 10:05 Uhr)
Windows Tipps & SecurityGarmin Cirqa im Test: Fitness-Tracker ohne Display(22.09.2026 um 10:30 Uhr)
Windows Tipps & SecuritySicher online bezahlen: 7 Methoden im Praxis-Check(22.09.2026 um 09:00 Uhr)
Sichere Programmierunghas_tokens: true is a boolean. 476 of 791 have no market behind them.(22.09.2026 um 10:00 Uhr)
Sichere ProgrammierungClaude Code Hooks – Safety Through Invariants(22.09.2026 um 10:04 Uhr)
Sichere ProgrammierungYour MCP Tool Just Returned a Secret. Did It Need To?(22.09.2026 um 10:05 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Understanding Scope, Hoisting, and Closures like a Pro!

🔹 What is Scope? Scope defines the accessibility of variables in your code. Simply put: Scope decides where in your code a variable can be used. In JavaScript, every variable has a “boundary.” Outside this boundary, the variable is unav…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

🔹 What is Scope?



Scope defines the accessibility of variables in your code.



Simply put:



Scope decides where in your code a variable can be used.



In JavaScript, every variable has a “boundary.” Outside this boundary, the variable is unavailable.



Why is Scope important?




  • Prevent variable conflicts

  • Manage memory efficiently

  • Make code predictable



Main types of Scope:



Global Scope → accessible from anywhere

Function Scope → accessible only within a function

Block Scope → accessible within {} (using let or const)

Lexical Scope → determined by the code’s written structure



🔹 Scope Example




let person = [1,2,3,4,5]; // global scope

function total(num1, num2) {
const result = num1 + num2; // function scope

if(true) {
var result1 = num1 * num2; // function scope (var)
}

console.log(result1); // accessible
console.log(person); // global access
}

total(10, 20);

console.log(result); // ❌ Error, function scope







Takeaways:




  • result is not accessible outside the function

  • result1 is accessible inside function due to var being function-scoped



🔹 What is Hoisting?



Hoisting is JavaScript’s behavior where variable and function declarations are moved to memory at the beginning of the execution phase.



In other words:



Before your code runs, JS prepares memory for all declared variables and functions.



Important points:



var → hoisted and initialized as undefined

let / const → hoisted but not initialized (they stay in the Temporal Dead Zone)

Function declarations → fully hoisted



Common misconception:



Some think hoisting literally “moves code to the top,” but technically, it’s a memory preparation process.



🔹 Hoisting Example




console.log(a); // undefined
var a = 10;







Internally, JavaScript interprets it as:




var a;
console.log(a); // undefined
a = 10;







Using let or const:




console.log(b); // ❌ ReferenceError
let b = 10;







This happens because let/const are hoisted but not initialized, creating the Temporal Dead Zone (TDZ).



🔹 What are Closures?



A closure is when a function “remembers” variables from its outer scope even after the outer function has finished executing.



Simply:



Function + its surrounding environment = Closure



Why closures are useful:



Maintain private data



Keep state between function calls



Widely used in real-world JS (event handlers, React hooks, etc.) 



🔹 Closure Example




function total() {
let counter = 0;

return function() {
counter++;
console.log(counter);
}
}

Usage:


const result1 = total();

result1(); // 1
result1(); // 2

const result2 = total();

result2(); // 1
result1(); // 3







Key points:



counter is not global



The inner function remembers its value



Each call to total() creates a new memory instance



🔹 Lexical Scope (The backbone of closures)





function outer() {
let a = 10;

return function inner() {
console.log(a);
}
}







The inner function is declared inside the outer function



Therefore, it can access variable a



This is lexical scope, which allows closures to work

How These Concepts Connect

Scope → defines where a variable lives

Lexical Scope → defines what variables a function can access based on the written code

Closure → allows functions to “remember” variables from outer scopes

All three work together in JavaScript!



Summary



Scope → variable boundaries

Hoisting → memory preparation before execution

Lexical Scope → determines access based on code structure

Closure → functions remember outer data



Final Thought



Many unexpected behaviors in JavaScript happen because these concepts are unclear.



Once Scope, Hoisting, Lexical Scope, and Closures are understood, JavaScript becomes predictable and much easier to work with

#webdevelopment #frontend #closure #hoisting #scope #lexicalscope #learntocode

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Understanding Scope, Hoisting, and Closures like a Pro!

Thematisch verwandte Begriffe: Understanding, Scope, Hoisting, Closures · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-94426 | A vulnerability was determined in xuxueli xxl-job up to 3.5.0. The impac…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick