JavaScript์—์„œ VAR, LET, & CONST

๋ฐœํ–‰: (2025๋…„ 12์›” 23์ผ ์˜คํ›„ 11:33 GMT+9)
3 ๋ถ„ ์†Œ์š”
์›๋ฌธ: Dev.to

Source: Dev.to

๐Ÿง  How JavaScript Runs Your Code (Very Important)

JavaScript doesnโ€™t execute your code lineโ€‘byโ€‘line immediately.
It first prepares everything, then runs it.

Memory Creation Phase

The JS engine:

  • Scans the entire code
  • Allocates memory for variables & functions
  • Does not assign actual values yet

Execution Phase

  • Executes code line by line
  • Assigns values
  • Runs functions

All of this happens inside an Execution Context.

๐Ÿงช var โ€” The OG Troublemaker

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

Result: undefined (not an error) because var is hoisted and initialized with undefined.

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 (functionโ€‘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

var ignores {} blocks, which caused many realโ€‘world bugs.

๐Ÿง  Enter let โ€” The Responsible One

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

Error: ReferenceError: Cannot access 'b' before initialization

Temporal Dead Zone (TDZ) โ€” Explained Simply

TDZ is the time between memory allocation and variable initialization.

Memory Phase

b โ†’

Execution Phase

console.log(b); // โŒ TDZ error
b = 20;         // TDZ ends here

The engine knows the variable exists but forbids access until itโ€™s initialized.

Block Scope (Finally!)

if (true) {
  let score = 100;
}
console.log(score); // โŒ ReferenceError

let respects block boundaries, making code safer and 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 (but stays in TDZ until initialization)
const x; // โŒ SyntaxError

The Famous const + Object Confusion

const user = { name: "Hardeep" };
user.name = "Singh"; // โœ… Allowed
user = {};           // โŒ Not allowed

const fixes the reference, not the objectโ€™s contentsโ€”think of a fixed address with movable furniture inside.

๐Ÿ“Š Quick Comparison Table

Featurevarletconst
ScopeFunctionBlockBlock
HoistedYesYesYes
Initialized on hoistingundefinedโŒ TDZโŒ TDZ
Redeclareโœ…โŒโŒ
Reassignโœ…โœ…โŒ
Recommended todayโŒ Noโœ… Yesโœ… Yes

๐Ÿง  Behind the Scenes (Slightly Nerdy)

  • Variable Environment โ†’ used by var
  • Lexical Environment โ†’ used by let & const

This is why var can leak out of blocks while let and const do not.

๐Ÿš€ Best Practices (Learned the Hard Way)

  • โœ… Use const by default
  • โœ… Use let only when reassignment is required
  • โŒ Avoid var unless maintaining 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 isnโ€™t weird; we just finally understand how it works.

0 ์กฐํšŒ
Back to Blog

๊ด€๋ จ ๊ธ€

๋” ๋ณด๊ธฐ ยป

JavaScript ์Šค์ฝ”ํ”„ & ํด๋กœ์ €: ์™ธ์šฐ๋Š” ๊ฑธ ๋ฉˆ์ถ”๊ณ , ์ดํ•ดํ•˜๊ธฐ ์‹œ์ž‘ํ•˜๋ผ

์†”์งํžˆ ๋งํ•ด๋ด…์‹œ๋‹ค. JavaScript ๊ธฐ์ˆ  ๋ฉด์ ‘์— ๋“ค์–ด๊ฐ€๋ฉด, ๋ฉด์ ‘๊ด€์€ Stack Overflow์—์„œ ๋ณต์‚ฌโ€‘๋ถ™์—ฌ๋„ฃ๊ธฐ ํ•  ์ˆ˜ ์žˆ๋Š” ์‚ฌ๋žŒ์„ ์ฐพ๋Š” ๊ฒƒ์ด ์•„๋‹™๋‹ˆ๋‹ค....

React, GSAP ๋ฐ Tailwind v4๋กœ ํ”„๋ฆฌ๋ฏธ์—„ ๋ฒค๋˜ ์Šคํƒ€์ผ ํฌํŠธํด๋ฆฌ์˜ค ๋งŒ๋“ค๊ธฐ

React, GSAP ๋ฐ Tailwind v4๋ฅผ ์‚ฌ์šฉํ•œ ํ”„๋ฆฌ๋ฏธ์—„ ๋ฒคํ†  ์Šคํƒ€์ผ ํฌํŠธํด๋ฆฌ์˜ค ๊ตฌ์ถ•์„ ์œ„ํ•œ ์ปค๋ฒ„ ์ด๋ฏธ์ง€ https://media2.dev.to/dynamic/image/width=1000,height=420,fit=cover,grav...

2์ฃผ ๋งŒ์— ํ”„๋ผ์ด๋ฒ„์‹œ ์šฐ์„  ํ†ตํ™” ๋ณ€ํ™˜๊ธฐ๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค

The Problem ๐Ÿคฆโ€โ™€๏ธ ๋‚ด๊ฐ€ ์‹œ๋„ํ•œ ๋ชจ๋“  currency converter๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฌธ์ œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์—ˆ๋‹ค: - โŒ ๊ฐ•์ œ signโ€‘ups์™€ email ์ˆ˜์ง‘ - โŒ ์ฟ ํ‚ค๋กœ user behavior ์ถ”์  - โŒ ๋А๋ฆฌ๊ฒŒ ๋กœ๋“œ๋จ ...