17 Variable Hoisting example

Last updated on July 13th, 2020 at 02:29 pm.

17 Variable Hoisting example - JavaScript Tutorial 2018

This video is taken from the full course that will teach you HTML, CSS, Programming concepts and Javascript .

Video Transcript:

Hi, and welcome back.

So if I come here to Firefox and then open Scratchpad, I already have this typed out here.

So in this case, you’re seeing that I’m calling β€˜x’ and β€˜x’ has been declared down here.

So if I run this, it’ll give me β€˜x’ is undefined.

And this is an example you are familiar with because you saw the same thing when we were looking at undefined and reference errors in JavaScript.

So if I run this, ok, this is because I already did run this with β€˜x’ up there.

So let me change, let me call β€˜y’, and then run this.


I also did run β€˜y’.

So let’s say I do β€˜z’ and I change this to β€˜z’ and then I run.

Let me close that and then I run.

You’ll see it gives us undefined.

So the reason the first time I did β€˜x’ and said they showed the values of β€˜5’ and β€˜10’ is because that is the second time I was running this code.

You know that by the second time I’m running the code, these variables are already stored in the memory of the browser.

So even if I run this a second time, and right now if I run this a second time, it’ll give me the value of β€˜z’ as β€˜5’ because by now, the value of β€˜z’ is already stored in the browser.

So if I run it, let me first of all close this undefined.

And if I run this, it’ll give us the value of β€˜z’ is equals to β€˜5’.

And this is because, the value of β€˜z’, by the second time I’m running this code, the value of β€˜z’ is a variable that is already stored within the memory of the browser.

So keep that in mind.


So we said that functions are also hoisted.

So this is a simple function and I have it commented out.

This is a simple function that adds β€˜3’ and β€˜4’.

So in the next video, let’s look at how this function will get hoisted to the top.

I will see you in that next video.

This section is from Learn Javascript from Scratch.

Learn Javascript from Scratch

Comment Here

Need WordPress help? Linux Server help? Talk to us.

  • We are your own WordPress customer service.
  • We set up Linux servers and install or migrate WordPress. Learn more here.
  • We support WooCommerce too.
  • Check out our WordPress customer support plans here or contact us below .

If you have any questions regarding WordPress support, Linux server support or any of our services, feel free to reach out or read more on our services page.

Join this free course:

How to host multiple WordPress
websites on a VPS

Close me!