getify / You-Dont-Know-JS

A book series on JavaScript. @YDKJS on twitter.
Other
178.87k stars 33.44k forks source link

TDZ - let and const hoisting issue #1611

Closed ratneshmurugesangale closed 4 years ago

ratneshmurugesangale commented 4 years ago

Edition: (2nd)

Book Title: You-Dont-Know-JS

Chapter: Chapter 5

Section Title: Scope-closures -> Uninitialized Variables (aka, TDZ)

Problem:

Declared variable studentName inside block-scope is not hoisted to top of section and below error has been raised.

Error Info: Uncaught ReferenceError: Cannot access 'studentName' before initialization at :4:17

var studentName = "Kyle"; { console.log(studentName); // ??? // .. let studentName = "Suzy"; console.log(studentName); // Suzy }

TDZ-let-const-hoisting-issue
getify commented 4 years ago

Declared variable studentName inside block-scope is not hoisted to top of section and below error has been raised.

Yes it is hoisted, that's the whole point of this code and the following 3-4 paragraphs explain exactly that.