Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Sichere ProgrammierungI audited my own ML linter and had to withdraw its best evidence(21.09.2026 um 22:54 Uhr)
Sichere ProgrammierungQuantum Result Validation for Distributed Computing Systems(21.09.2026 um 22:54 Uhr)
Sichere ProgrammierungJWT Authentication and Role-Based Access Control in LocalHands(21.09.2026 um 22:56 Uhr)
Sichere ProgrammierungStochastic Parrot or Alien Mind?(21.09.2026 um 22:56 Uhr)
Sichere ProgrammierungBuilding AI for the Physical World Is a Different Engineering Problem(21.09.2026 um 22:58 Uhr)
Sichere ProgrammierungI audited my own ML linter and had to withdraw its best evidence(21.09.2026 um 22:54 Uhr)
Sichere ProgrammierungQuantum Result Validation for Distributed Computing Systems(21.09.2026 um 22:54 Uhr)
Sichere ProgrammierungJWT Authentication and Role-Based Access Control in LocalHands(21.09.2026 um 22:56 Uhr)
Sichere ProgrammierungStochastic Parrot or Alien Mind?(21.09.2026 um 22:56 Uhr)
Sichere ProgrammierungBuilding AI for the Physical World Is a Different Engineering Problem(21.09.2026 um 22:58 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

VAR, LET, & CONST in JavaScript

🤹‍♂️ var, let, and const in JavaScript Three variables walk into a scope… and only two behave properly If you’ve worked with JavaScript, you’ve definitely met these three: var let const They look similar. They behave v…

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




🤹‍♂️ var, let, and const in JavaScript






Three variables walk into a scope… and only two behave properly



If you’ve worked with JavaScript, you’ve definitely met these three:




var
let
const






They look similar.

They behave very differently.

And one of them (var) is responsible for a lot of emotional damage in production systems 😄



Let’s understand what really happens behind the scenes, without jargon overload and without falling asleep.







🧠 How JavaScript Runs Your Code (Very Important)



JavaScript doesn’t execute your code line by line immediately.

It first prepares everything, then runs it.





JavaScript runs in two phases:





1️⃣ Memory Creation Phase



The JS engine:




  • Scans the entire code

  • Allocates memory for variables & functions

  • Does not assign actual values yet





2️⃣ Execution Phase




  • Executes code line by line

  • Assigns values

  • Runs functions



This all happens inside something called an Execution Context.







🧪 var — The OG Troublemaker



Let’s start with an example everyone has seen:




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









Expected by beginners:



❌ Error






Given by JavaScript:



undefined



Why? Because JavaScript is… forgiving.






Behind the scenes 👇



Memory Phase




a  undefined






Execution Phase




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









Key takeaways about var




  • ✅ Hoisted

  • ✅ Initialized with undefined

  • ❌ Not block-scoped

  • ❌ Can be redeclared

  • ❌ Easy to mess up









🚨 var Does Not Respect Block Scope






if (true) {
var secret = "I should not escape";
}

console.log(secret); // 😱 It escaped






Why?





  • var is function-scoped


  • {} blocks mean nothing to it



This behavior caused real bugs in real applications, which is why let and const were introduced.









🧠 Enter let — The Responsible One






console.log(b);
let b = 20;






❌ Error:




ReferenceError: Cannot access 'b' before initialization









But wait… isn’t let hoisted?



👉 Yes, it is hoisted. Plot twist.



So why the error?









⏳ Temporal Dead Zone (TDZ) — Explained Simply



TDZ is the time between:




  • Memory allocation

  • Variable initialization






Memory Phase






b  <uninitialized>









Execution Phase






console.log(b); // ❌ TDZ error
b = 20; // TDZ ends here






The JS engine basically says:




“I know you exist… but don’t touch me yet.”










🔐 Block Scope (Finally!)






if (true) {
let score = 100;
}

console.log(score); // ❌ ReferenceError






Unlike var, let respects block boundaries.

Much safer. Much cleaner.









🔒 const — The Strict One






const pi = 3.14;
pi = 3.14159; // ❌ TypeError






Rules of const:




  • Must be initialized immediately

  • Cannot be reassigned

  • Block-scoped

  • Hoisted (with TDZ)




const x; // ❌ SyntaxError






JavaScript wants commitment here.









🤯 The Famous const + Object Confusion






const user = { name: "Hardeep" };

user.name = "Singh"; // ✅ Allowed
user = {}; // ❌ Not allowed






Why?



Because const means:




“The reference cannot change, not the contents.”




Think:




  • Address is fixed

  • Furniture inside can move









📊 Quick Comparison Table


















































Feature var let const
Scope Function Block Block
Hoisted Yes Yes Yes
Initialized on hoisting undefined ❌ TDZ ❌ TDZ
Redeclare
Reassign
Recommended today ❌ No ✅ Yes ✅ Yes








🧠 Behind the Scenes (Slightly Nerdy)



Internally, JavaScript maintains:





  • Variable Environmentvar


  • Lexical Environmentlet & const



That’s why:





  • var leaks scope


  • let and const don’t









🚀 Best Practices (Learned the Hard Way)




  • ✅ Use const by default

  • ✅ Use let only when reassignment is required

  • ❌ Avoid var unless you’re working on legacy code



If JavaScript were designed today, var probably wouldn’t exist.









🎯 Final Thoughts





  • var → risky and outdated


  • let → safe and flexible


  • const → safest and preferred



JavaScript didn’t get weird.

We just finally understood how it works 😄



So, whether you're seeking JS advice, a good laugh, or simply a friendly chat about code and coffee preferences, I'm just a click away on these cloud-astic platforms.



LinkedIn: 🚀💼 hardeepjethwani@LinkedIn



TopMate: ☕🤝 hardeepjethwani@topmate



Instagram: 📸🌩️ hardeepjethwani@Instagram



X: 🐦☁️ hardeepjethwani@X

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten VAR, LET, & CONST in JavaScript

Thematisch verwandte Begriffe: CONST, JavaScript · 6 Treffer

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-79918 | MaxKB is an open-source AI assistant for enterprise. Prior to version 2.…
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