๐Ÿคทโ€โ™€๏ธ ๊ฐœ๋…๊ณต๋ถ€: 1. Javascript (const, let, var ์ฐจ์ด, ํ˜ธ์ด์ŠคํŒ…(hoisting)์ด๋ž€?)

Ko Seoyoungยท2021๋…„ 2์›” 9์ผ
0

const, let, var๊ฐ€ ๋ญ˜๊นŒ? ์–ด๋–ป๊ฒŒ ๋‹ค๋ฅผ๊นŒ?

์šฐ๋ฆฌ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ const, let, var๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•œ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์ด ์„ธ๊ฐ€์ง€ ๊ตฌ๋ฌธ์˜ ์ฐจ์ด์ ์€ ๋ฌด์—‡์ผ๊นŒ?

var์€ ๋ณ€์ˆ˜ ์„ ์–ธ ๋ฐฉ์‹์—์„œ ํฐ ๋‹จ์ ์ด ์žˆ๊ณ ,
const์™€ let์€ ES6 ์ดํ›„ ์ด๋ฅผ ๋ณด์™„ํ•˜๊ธฐ ์œ„ํ•ด ์ถ”๊ฐ€๋œ ๋ณ€์ˆ˜ ์„ ์–ธ ๋ฐฉ์‹์ด๋‹ค.

๐Ÿ‘‰ ์ฐจ์ด์  ์š”์•ฝ

var: ์žฌ์„ ์–ธใ…‡, ์žฌํ• ๋‹นใ…‡, function-scoped
let: ์žฌ์„ ์–ธx, ์žฌํ• ๋‹นใ…‡, block-scoped
const: ์žฌ์„ ์–ธx, ์žฌํ• ๋‹นx, block-scoped


1๏ธโƒฃ var, let

ES6(ES2015)์ „์—๋Š” var๊ฐ€ ์œ ์ผํ•œ ๋ณ€์ˆ˜ ์„ ์–ธ ๋ฐฉ์‹์ด์˜€๋‹ค.
ํ•˜์ง€๋งŒ var์˜ ๋‘๊ฐ€์ง€ ํŠน์ง•์ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฌธ์ œ์ ์„ ๊ฐ€์ ธ์˜จ๋‹ค.

1) var์€ ์žฌ์„ ์–ธ์ด ๊ฐ€๋Šฅํ•˜๋‹ค

var language = 'javascript'
console.log(language) // javascript

var language = 'react'
console.log(language) // react

๋ณ€์ˆ˜๋ฅผ ๋‹ค์‹œ ์„ ์–ธํ•ด๋„ ์—๋Ÿฌ๊ฐ€ ๋‚˜์ง€ ์•Š๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

์ด๋Š” ์ฝ”๋“œ๋Ÿ‰์ด ๋งŽ์•„์ง€๋ฉด ์ด์ „์— ์„ ์–ธ๋œ ๋ณ€์ˆ˜๋ฅผ ์ž˜๋ชป ์žฌ์„ ์–ธํ•˜๋Š” ๊ฒฝ์šฐ์ž„์—๋„ ์˜ค๋ฅ˜๋ฅผ ๋ฐœ๊ฒฌํ•˜๊ธฐ ์–ด๋ ค์›Œ์ง€๋Š” ๋ฌธ์ œ์ ์ด ์žˆ๋‹ค.

๋ฐ˜๋ฉด, ์ด๋Ÿฌํ•œ ๋ฌธ์ œ์ ์„ ๊ฐœ์„ ํ•˜๊ณ ์ž let์€ var์™€ ๋‹ฌ๋ฆฌ ์žฌ์„ ์–ธ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.

let language = 'javascript'
console.log(language) // javascript

let language = 'react'
console.log(language)
// Uncaught SyntaxError: Identifier 'name' has already been declared

2) var์€ ๋ธ”๋ก์ด ์•„๋‹Œ ํ•จ์ˆ˜ ๋ฒ”์œ„(function-scoped)์ด๋‹ค.

์ง€์—ญ๋ฒ”์œ„๊ฐ€ ๋ธ”๋ก๋‹จ์œ„์ธ c, c++์™€ ๊ฐ™์€ ๋Œ€๋ถ€๋ถ„์˜ ์–ธ์–ด์™€ ๋‹ฌ๋ฆฌ var๋Š” ํ•จ์ˆ˜๋ฒ”์œ„์ด๊ธฐ ๋•Œ๋ฌธ์— ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์‹œ ํ˜ผ๋™์„ ์ค„ ์ˆ˜ ์žˆ๋‹ค.

function hi(){
  for(var i = 0; i < 5 ;i++){
    console.log(i) // 1, 2, 3, 4
    if(true){
      var color = "red"
    }
  }
  console.log(i) // 5
  console.log(color) // red
}

console.log(i) // error
console.log(color) // error

์œ„ ์˜ˆ์ œ์˜ ๊ฒฝ์šฐ๋„ i๋ณ€์ˆ˜๋Š” for๋ฌธ, color ๋ณ€์ˆ˜๋Š” if๋ฌธ ๋ฐ–์—์„œ๋Š” ์ ‘๊ทผ๋  ์ˆ˜ ์—†์–ด์•ผ ํ•˜์ง€๋งŒ, var๋ฅผ ์‚ฌ์šฉํ•œ ๊ฒฝ์šฐ ์ด ๋ณ€์ˆ˜๋“ค์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๋ฌธ์ œ์ ์ด ์žˆ๋‹ค.

let์€ ํ•จ์ˆ˜๊ฐ€ ์•„๋‹Œ ๋ธ”๋ก ๋ฒ”์œ„์ด๊ธฐ ๋•Œ๋ฌธ์— ์ด ๊ฒฝ์šฐ ์•„๋ž˜์™€ ๊ฐ™์ด ReferenceError๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

function hi(){
  for(let i = 0; i < 5 ;i++){
    console.log(i) // 1, 2, 3, 4
    if(true){
      let color = "red"
    }
  }
  console.log(i) // ReferenceError
  console.log(color) // ReferenceError
}

๊ฐ™์€ ๋ธ”๋ก ์•ˆ, ์žฌ์„ ์–ธ์€ ๋ถˆ๊ฐ€๋Šฅ, ์žฌํ• ๋‹น์€ ๊ฐ€๋Šฅ


2๏ธโƒฃ const

const๊ฐ€ var์™€ let์™€ ๋‹ค๋ฅธ ์ ์€ ์—ฌ๋Ÿฌ๋ฒˆ ์žฌํ• ๋‹น ๋  ์ˆ˜ ์žˆ๋Š” var, let๊ณผ ๋‹ฌ๋ฆฌ const๋Š” ํ•œ๋ฒˆ๋งŒ ํ• ๋‹น๋  ์ˆ˜ ์žˆ๋Š” ์ƒ์ˆ˜๋ผ๋Š” ๊ฒƒ์ด๋‹ค.

var language = 'javascript'
let language2 = 'react'
const language3 = 'react-native'
const temp;
// Uncaught SyntaxError: Missing initializer in const declaration

language = 'javascript'
language2 = 'react'
language3 = 'react-native'
// Uncaught TypeError: Assignment to constant variable.

๋”ฐ๋ผ์„œ const๋Š” ์ดˆ๊ธฐ๊ฐ’์ด ์—†๋Š” ๊ฒฝ์šฐ์™€ ์žฌํ• ๋‹น์ด ๋˜๋Š” ๊ฒฝ์šฐ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.


Refs

var, let, const ์ฐจ์ด์ 


ํ˜ธ์ด์ŠคํŒ…(hoisting)์ด ๋ญ˜๊นŒ?

ํ˜ธ์ด์ŠคํŒ…(Hoisting)์ด๋ž€, ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋˜๊ธฐ ์ „์— var ์„ ์–ธ๋ฌธ์ด๋‚˜ function ์„ ์–ธ๋ฌธ ๋“ฑ์ด ํ•ด๋‹น ์Šค์ฝ”ํ”„์˜ ์„ ๋‘๋กœ ์˜ฎ๊ฒจ์ง„ ๊ฒƒ์ฒ˜๋Ÿผ ๋™์ž‘ํ•˜๋Š” ํŠน์„ฑ์„ ๋งํ•œ๋‹ค.

ํ•จ์ˆ˜ ํ˜ธ์ด์ŠคํŒ…์ด ๋ฐœ์ƒํ•˜๋Š” ์›์ธ์€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋ณ€์ˆ˜ ์ƒ์„ฑ(Instantiation)๊ณผ ์ดˆ๊ธฐํ™”(Initialization)์˜ ์ž‘์—…์ด ๋ถ„๋ฆฌ๋ผ์„œ ์ง„ํ–‰๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ES6์—์„œ ๋„์ž…๋œ let, const๋ฅผ ํฌํ•จํ•˜์—ฌ ๋ชจ๋“  ์„ ์–ธ(var, let, const, function, function*, class)์„ ํ˜ธ์ด์ŠคํŒ…ํ•œ๋‹ค.

๐Ÿ‘‰ undefined vs ReferenceError

// test1.js
console.log(a) // undeined
console.log(b) // undeined
var a = "Hello"
var b = "world"
// test2.js
console.log(a) // ReferenceError

ํ˜ธ์ด์ŠคํŒ…์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ธฐ ์ „ undefined์™€ ReferenceError์˜ ์ฐจ์ด์ ์€ ๋ฌด์—‡์ผ๊นŒ?

  • undefined: ์•„์ง ๊ฐ’์ด ํ• ๋‹น๋˜์ง€ ์•Š๊ณ  ์„ ์–ธ๋งŒ ๋œ ๋ณ€์ˆ˜์˜ ๊ฐ’, ์„ ์–ธ๋˜์ง€ ์•Š์€ ๋ณ€์ˆ˜์˜ ํƒ€์ž…๊ณผ ๊ฐ’
  • ReferenceError: ์ด์ „์— ์„ ์–ธ๋˜์ง€ ์•Š์€ ๋ณ€์ˆ˜์— ์ ‘๊ทผํ•˜๋ ค ํ•  ๋•Œ ๋ฐœ์ƒ

๊ทธ๋ ‡๋‹ค๋ฉด test1์˜ ๊ฒฝ์šฐ, a์™€ b๊ฐ€ ReferenceError๋ฅผ ๋ฐœ์ƒํ•˜์ง€ ์•Š์•˜์œผ๋ฏ€๋กœ a,b๊ฐ€ ์ด๋ฏธ ์„ ์–ธ๋˜์—ˆ๋‹ค๋Š” ๋œป์ธ๋ฐ ์–ด๋–ป๊ฒŒ ๋œ ๊ฒƒ์ผ๊นŒ??

var์˜ ํ˜ธ์ด์ŠคํŒ…

์šฐ๋ฆฌ๋Š” ๋ณดํ†ต var a = 10 ์ด์™€ ๊ฐ™์ด ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•œ๋‹ค. ํ•˜์ง€๋งŒ ๊ทธ ๋’ท๋ฉด์—์„œ javascript๋Š” ๋ชจ๋“  ๋ณ€์ˆ˜์™€ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ด์ŠคํŒ…ํ•˜๊ณ  ์žˆ๋‹ค.

๋”ฐ๋ผ์„œ test1์˜ ๊ฒฝ์šฐ์—์„œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์•˜๋˜ ๊ฒƒ์ด๋‹ค!
์ฆ‰, test1์˜ ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ๋™์ž‘ํ•œ๋‹ค.
(์ดˆ๊ธฐํ™”๊ฐ€ ์•„๋‹Œ ์„ ์–ธ๋งŒ ๋Œ์–ด์˜ฌ๋ฆผ)

var a;
var b;
console.log(a) // undeined
console.log(b) // undeined
a = "Hello"
b = "world"

  • ํ•˜์ง€๋งŒ let๊ณผ const๋Š” ํ˜ธ์ด์ŠคํŒ… ๋˜์ง€๋งŒ ์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒํ•œ๋‹ค.
carName = "Volvo";
let carName;

var๋กœ ์„ ์–ธ๋œ ๋ณ€์ˆ˜์™€ ๋‹ฌ๋ฆฌ let์œผ๋กœ ์„ ์–ธ๋œ ๋ณ€์ˆ˜๋Š” ์„ ์–ธ๋  ๋•Œ๊นŒ์ง€ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ, ์„ ์–ธ๋ฌธ ์ด์ „์— ์ฐธ์กฐํ•˜๋ฉด ReferenceError๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

carName = "Volvo";
const carName;

const์„ ์–ธ๋˜๊ธฐ ์ „์— ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ๊ตฌ๋ฌธ ์˜ค๋ฅ˜์ด๋ฏ€๋กœ ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค.


function์˜ ํ˜ธ์ด์ŠคํŒ…

sayHello(); // Output: 'Hello'

function sayHello() {
  console.log('Hello');
};

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š” ์ปดํŒŒ์ผ ๋‹จ๊ณ„์—์„œ ํ˜ธ์ด์ŠคํŒ…๋œ ๋ณ€์ˆ˜์™€ ํ•จ์ˆ˜๋“ค์„ ๋ชจ๋‘ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ํ•จ์ˆ˜์˜ ๊ฒฝ์šฐ๋„ var์™€ ๊ฐ™์ด ๋งจ์œ„๋กœ ์„ ์–ธ์ด ๋˜๋ฏ€๋กœ ์œ„ ์ฝ”๋“œ์—์„œ sayHello๋Š” ์•„๋ž˜ ์„ ์–ธ๋œ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜์—ฌ 'Hello'๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

ํ•˜์ง€๋งŒ ์ด๋Ÿฌํ•œ ์ด์œ (ํ•จ์ˆ˜๋ฅผ ๋ชจ๋‘ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅํ•˜๋ฏ€๋กœ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋‚ญ๋น„ ๋จ)๋กœ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ „๋ฌธ๊ฐ€๋“ค์€ ํ•จ์ˆ˜ ์ƒ์„ฑ์— ์žˆ์–ด์„œ ํ•จ์ˆ˜ ์„ ์–ธ์‹(Function declarations)๋ณด๋‹ค ํ˜ธ์ด์ŠคํŒ…์— ์˜ํ–ฅ์„ ๋ฐ›์ง€ ์•Š๋Š”ํ•จ์ˆ˜ ํ‘œํ˜„์‹(Function expressions)๋งŒ์„ ์‚ฌ์šฉํ•  ๊ฒƒ์„ ๊ถŒ์žฅํ•˜๊ณ  ์žˆ๋‹ค.

ํ•จ์ˆ˜ํ‘œํ˜„์‹์„ ์ด์šฉํ•˜์—ฌ ์œ„ ์ฝ”๋“œ๋ฅผ ๋‹ค์‹œ ์ž‘์„ฑํ•ด๋ณด์ž.

sayHello(); // Uncaught TypeError: sayHello is not a function

var sayHello = function() {
  console.log('Hello');
};

์ด ๊ฒฝ์šฐ var = sayHello๋งŒ ์ƒ๋‹จ๋ถ€๋กœ ํ˜ธ์ด์ŠคํŒ…์ด ๋˜๋ฏ€๋กœ ์‹ค์งˆ์ ์ธ ํ•จ์ˆ˜ ๋‚ด์šฉ์€ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅ๋˜์ง€ ์•Š์•„ ๋ฉ”๋ชจ๋ฆฌ ๋‚ญ๋น„๋ฅผ ์ค„์ธ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•จ์ˆ˜ ํ‘œํ˜„์‹์œผ๋กœ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•  ๊ฒฝ์šฐ sayHello()๋Š” undefined()๋กœ TypeError๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ˜๋“œ์‹œ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•œ ๋‹ค์Œ์— ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.


Refs

Understanding Hoisting in JavaScript

18: Hoisting in JavaScript Explained | What is Hoisting in JavaScript | JavaScript Tutorial

Hoisting in JavaScript Explained in (About) 5 Minutes

profile
Web Frontend Developer ๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป #React #Nextjs #ApolloClient

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