How JavaScript Reads Your Code Before Running It
JavaScript does a quick scan of your code before it runs, noting all variable and function names. Think of it like a teacher writing students’ names on the board before they enter the classroom.
Function declarations are fully prepared during this scan, so you can call them before they appear in your code. Variables declared with var are also registered, but they start with an empty value (undefined).
Variables declared with let and const are different. They are reserved ahead of time, but remain locked until JavaScript reaches their declaration line. If you try to use them too early, JavaScript throws an error.
Understanding hoisting explains many confusing JavaScript moments. More on this in my upcoming book: Advanced JavaScript for Newbies — Teach Me JS Like I’m 5.