상세 컨텐츠

본문 제목

[JS] 예약어 오류가 다른 이유

TIL (Today I Learned)

by NayC 2021. 11. 29. 19:41

본문

728x90

 

 

var, let 둘 다 예약어가 아니던가? 왜 둘의 오류 메시지가 다를까.

Uncaught SyntaxError: var is disallowed as a lexically bound name

Uncaught SyntaxError: let is disallowed as a lexically bound name 

 

해결

es6가 나오기 전에 let은 완벽히 변수가 될 수 있었음.

-> 그래서 let만이 유일하게 예약어가 아니었던 적이 있어서 에러메시지가 다르다고 한다.

 

예를 들어) var let = 3의 경우에는 올바른 문법임. 

 

 

let declarations didn't exist prior to the ES2015 standard, so let could be used freely as a variable name. Changing the language to stop allowing that variable name could have broken applications which were written before ES2015 existed, at a time when let was a perfectly good variable name. (Which covered a period of twenty years -- 1995 to 2015!)
Since let was a new feature, there was no existing code to break, and disallowing let as a variable name avoided a potential source of syntactic confusion.

출처: https://stackoverflow.com/questions/50789375/var-let-is-ok-but-let-let-isnt/50789417

궁금해서 제로초님에게도 물어봐서 확인 완료.

728x90
반응형

관련글 더보기