Hoisting in JavaScript

If you are learning JavaScript and wondering what on earth is hoisting, read on.

Hoisting is simply pulling a variable/ named function to the top and have them be available to the entire code. Before running all the code, the browser first gets all declarations, hoisting is the name of this process.

Let’s look at an example:

Example 1:

console.log(d);var d;//undefined

The reason that d is undefined is because d has been declared but not been assigned to a value. Here, d has been hoisted to the top.

If we assign d to a value at the top before the console log, it will print out the value of d which in this case is 4.

d = 4console.log(d);var d;//4

That, in a nutshell, is hoisting.

Happy coding!



For my coding challenge at Flatiron School during my Rails Project review, I was tasked to add a search box in one of my models index view. Here’s a quick capture of my rails project models set up:

This web app has 3 models:


  • has many behavior logs
  • has…