Javascript를 공부하는 사람이라면 변수선언 키워드인 var, let, const의 차이를 다들 알고 있을 거라 생각합니다.
여러 차이점이 있지만 키워드별 scope의 차이의 개념을 접했을 때 감이 잘 잡히지 않았어요
var 는 함수레벨 scope를 , let과 const 블록레벨 scope를 가지죠.
아래 Code를 보면 단번에 이해 되실 거에요.
var 로 선언된 변수 scopeVar는 if{ } 블록밖에서도 그 값이 유효하다는 걸 알 수 있어요 ( 단, f2(){ } 함수내에서만 )
반면에 let과 const로 선언된 변수는 if{ } 라는 블록 밖에서는 값이 유효하지 않아요
'Javascript' 카테고리의 다른 글
프로그래머스 코딩테스트 풀이(js) > 숫자 변환하기 (lv2) (0) | 2023.06.11 |
---|---|
프로그래머스 코딩테스트 풀이(js) > 최댓값과 최솟값 (lv2) (0) | 2023.06.04 |
[Javascript] es6 import, export 예시 (0) | 2022.07.10 |
[Javascript] Prototype, __proto__ (0) | 2022.07.09 |
[javascript] scroll 위치에 따른 navi bar 변경하기 (0) | 2022.06.22 |