16 Variable and func hoisting – JavaScript for Beginners

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

16 Variable and function hoisting - JavaScript Tutorial 2018

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

Video Transcript:

Hey, and welcome.

So in this video, we are going to look at hoisting, variable hoisting.

So the exact meaning of ‘to hoist’ is to lift or to raise to the top, the same way you hoist a flag.

So in the same sense, variable hoisting means that a variable will be raised to the top, once it is declared.

If you declare a variable down here and you call that variable up here, the variable will be hoisted meaning that if you declare ‘x’ down here and you try to use ‘x’ up here, this variable is always hoisted to the top.

Although, if you try to call this variable and use it and try to get its value, the value will be ‘undefined’.

And we did see this when we were looking at undefined and reference error.

So, a variable is always hoisted to the top and it’s very important to note that only variables that are declared with the ‘var’ keyword are hoisted to the top.

If you declare a variable with the ‘let’ keyword, it will not get hoisted.

And if it is also a ‘const’ it will not get hoisted.

We’ll look at ‘const’ later on.

Functions are also hoisted.

So if you declare a function, it will also be hoisted to the top.

And this is, this is similar to a bunch of ‘C-based’ programming languages.

Even PHP, you can declare a function later on and call it before even the function declaration.

As long as the function is there, it will work just fine.

So in JavaScript, function hoisting only works for named functions.

So a ‘named function’ is a function that has a name.

So it has to be defined or declared as function.

And then, the name of the function.

And then, whatever you want to do with the function will be in there.

So it has to be a named function.

There is no way to hoist anonymous functions and function expressions.

So a ‘function expression’ is a, for example if you declare a function and you want to store it in a variable, you can say var declare is equals to a function in here.

So in the next example, in the next video I mean, we’re going to look at variable hoisting and function hoisting in one simple example.

So I’ll 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!