Skip to main content

Javascript Hoisting, explained.

Published 2021/07/19 13:00

What is Hoisting in Javascript?

Let's explain!

Suppose you're writing a function in Javascript. A very simple function that just prints "Hello" to the console:

let sayHello = function(){
    console.log("Hello");
}

Now to actually make the function get "Hello" to print in the console, you'll need to call the function. It makes sense that you'll call the function after it's been defined right?

let sayHello = function(){
    console.log("Hello");
}

sayHello();

Now you're reading about how to make your code more readable and cleaner and you hear about calling the functions above the function itself. In other words:

sayHello();

let sayHello = function(){
    console.log("Hello");
}

But what would happen if you execute this code?

You'll get this error:

Uncaught ReferenceError: sayHello is not defined

Which makes sense! You can't just call a function before it's defined! Right?

We need a way to define the function as soon as the script runs so it's available for me to run wherever I need.

This thing is possible with a technique called Hoisting!

Let's have another look at the function and notice how it is defined:

let sayHello = function(){
    console.log("Hello");
}

It is defined using the let keyword. This means that the function is defined at that point. Not before - not after. To make this work…

sayHello();

let sayHello = function(){
    console.log("Hello");
}

You'll need to give it priority, lift it at the top of the queue. In other words you need to hoist it.

How do you do that?

Simply change your function definition to this:

function sayHello(){
    console.log("Hello");
}

Now when you call the sayHello() function above the definition:

sayHello();

function sayHello(){
    console.log("Hello");
}

You'll get the desired result!

> Hello

And that's how hoisting works in simple words. Thank you for reading. I hope this has made hoisting slightly easier. πŸ‘‹πŸ‘‹πŸ‘‹

Dev, Explained (43 part series)

  1. Javascript Scopes, explained.
  2. Javascript Promises, explained.
  3. Accessibility, explained.
  4. React, explained
  5. Should I use forEach() or map()?
  6. Should I use Flexbox or CSS Grid?
  7. Docker, explained.
  8. Unit testing, explained
  9. Git, explained.
  10. Typescript, explained.
  11. async/await, explained.
  12. The DOM, explained.
  13. Regular expressions, explained
  14. GraphQL, explained.
  15. Vue, explained.
  16. Svelte, explained.
  17. API, explained.
  18. Javascript Hoisting, explained.
  19. Immediately Invoked Function Expressions (IIFE), explained.
  20. ARIA roles, explained.
  21. Test-driven Development, explained.
  22. ARIA live regions, explained.
  23. aria-label in accessibility, explained.
  24. Type coercion in Javascript, explained.
  25. Variables, explained.
  26. if statements, explained.
  27. Arrays, explained.
  28. Currying in Javascript, explained.
  29. Memoization, explained.
  30. For loops, explained.
  31. Javascript Prototypes, explained.
  32. React Hooks, explained.
  33. Graph databases, explained.
  34. MongoDB, explained.
  35. Serverless, explained.
  36. Javascript Callback functions, explained.
  37. HTML, explained.
  38. CSS, explained.
  39. Responsive design, explained.
  40. Javascript, explained.
  41. The CSS Box Model, explained.
  42. CSS Flexbox, explained.
  43. CSS Grid, explained.
2022 Savvas Stephanides
Buy me a coffee
Some icons from Freepik