From the course: JavaScript: Scope

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Hoisting explained

Hoisting explained - JavaScript Tutorial

From the course: JavaScript: Scope

Start my 1-month free trial

Hoisting explained

- [Instructor] Hoisting is often misunderstood so let's focus some time on this mysterious word and what does it mean. In short, hoisting is JavaScript's behavior of moving all declarations to the top of its scope. When you declare a variable at the bottom of your file it will be hosted or moved to the top of its scope when the code is compiled. Let me demonstrate with some code. I'm gonna put some notes here so you understand what's happening. Let's remove the declaration here and simply define what warrior is here. We need to declare it somewhere. Warrior is defined here. Then, let's put a note here. And this is for closure, so I'm gonna write a note here for you guys. Wrap in a function now is a closure, there you go. Let's declare our warrior way below, down here. What I'm gonna do is leave those two here and then do a console.log for warrior and warrior3. But the third warrior hasn't been declared yet, so let's…

Contents