๐Ÿ“’Javascript ๋ณ€์ˆ˜ :)

zooyahoยท2022๋…„ 4์›” 6์ผ
0

study with me

๋ชฉ๋ก ๋ณด๊ธฐ
6/19
post-thumbnail

1. ๋ณ€์ˆ˜์˜ ์ƒ์„ฑ๊ณผ์ •

๐Ÿ“Œ ์ดˆ๊ธฐํ™”: undefined๋ฅผ ํ• ๋‹น ํ•ด์ฃผ๋Š” ๋‹จ๊ณ„

๐Ÿ”ต var

  1. ์„ ์–ธ ๋ฐ ์ดˆ๊ธฐํ™” ๋‹จ๊ณ„
  2. ํ• ๋‹น ๋‹จ๊ณ„

๐Ÿ”ต let

  1. ์„ ์–ธ ๋‹จ๊ณ„
  2. ์ดˆ๊ธฐํ™” ๋‹จ๊ณ„
  3. ํ• ๋‹น ๋‹จ๊ณ„

๐Ÿ”ต const

  1. ์„ ์–ธ + ์ดˆ๊ธฐํ™” + ํ• ๋‹น

๐Ÿ‘พ#1

let name;
name = 'Zooyaho';

var age;
age = 30;

const gender; // SyntaxError
gender = 'male';

2. ํ˜ธ์ด์ŠคํŒ…(Hoisting)

์Šค์ฝ”ํ”„ ๋‚ด๋ถ€ ์–ด๋””์„œ๋“  ๋ณ€์ˆ˜ ์„ ์–ธ์€ ์ตœ์ƒ์œ„์— ์„ ์–ธ๋œ ๊ฒƒ ์ฒ˜๋Ÿผ ํ–‰๋™

3. ๋ณ€์ˆ˜ ์„ ์–ธ

var, let, const

๐Ÿ”ต var

  • var๋Š” ์„ ์–ธํ•˜๊ธฐ ์ „์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
console.log(name); // undefined
var name = 'Zooyaho';

๐Ÿ‘‰๐Ÿป ์•„๋ž˜์™€ ๊ฐ™์ด ๋™์ž‘ํ•จ.

var name;
console.log(name); // undefined
name = 'Zooyaho';

๐Ÿ‘‰๐Ÿป ์„ ์–ธ์€ ํ˜ธ์ด์ŠคํŒ…์ด ๋˜์ง€๋งŒ ํ• ๋‹น์€ ํ˜ธ์ด์ŠคํŒ…์ด ๋˜์ง€ ์•Š์Œ~!

๐Ÿ”ต let, const

  • TDZ์— ์˜ํ–ฅ์„ ๋ฐ›๊ธฐ ๋•Œ๋ฌธ์— let, const๋Š” ์„ ์–ธํ•˜๊ธฐ ์ „์— ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Œ.
  • ํ˜ธ์ด์ŠคํŒ…์€ ๊ฐ€๋Šฅํ•จ, ์ดˆ๊ธฐํ™”๊ฐ€ ๋˜์ง€ ์•Š์•„ ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ.
console.log(name); // ReferenceError
let name = 'Zooyaho';

4. TDZ(Temporal Dead Zone)

  • ํ• ๋‹นํ•˜๊ธฐ ์ „์—๋Š” ์„ ์–ธ๋œ ๋ณ€์ˆ˜๋ฅผ ํ˜ธ์ด์ŠคํŒ…ํ•  ์ˆ˜ ์—†์Œ

๐Ÿ‘พ#1

let age = 12;
function showAge() {
  console.log(age); // ReferenceError
  let age = 20;
  
}
showAge();

๐Ÿ‘‰๐Ÿป ํ˜ธ์ด์ŠคํŒ…์€ ์Šค์ฝ”ํ”„ ๋‹จ์œ„๋กœ ์ผ์–ด๋‚˜๊ธฐ ๋•Œ๋ฌธ์— ์—๋Ÿฌ๋ฐœ์ƒ

5. ์Šค์ฝ”ํ”„

โ—พ๏ธ var: ํ•จ์ˆ˜ ์Šค์ฝ”ํ”„(function-scoped)

๐Ÿ‘‰๐Ÿป ํ•จ์ˆ˜ ๋‚ด์—์„œ ์„ ์–ธํ•  ๊ฒฝ์šฐ ํ•จ์ˆ˜ ๋ฐ–์—์„œ๋Š” ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ๋ชปํ•จ

โ—พ๏ธ let, const: ๋ธ”๋ก ์Šค์ฝ”ํ”„(block-scoped)

๐Ÿ‘‰๐Ÿป ํ•จ์ˆ˜, if๋ฌธ, for๋ฌธ, while๋ฌธ, try/catch๋ฌธ ๋“ฑ์˜ ์•ˆ์—์„œ๋งŒ ์„ ์–ธํ•œ ๋ณ€์ˆ˜ ์‚ฌ์šฉ๊ฐ€๋Šฅ

profile
์ฆ๊ฒ๊ฒŒ ๊ฐœ๋ฐœํ•˜์ž ์ฅฌ์•ผํ˜ธ๐Ÿ‘ป

0๊ฐœ์˜ ๋Œ“๊ธ€