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:

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.

That, in a nutshell, is hoisting.

Happy coding!

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Web components: from zero to hero

Cosmos Lottery⚛️

Revisiting Javascript

Create and Print Linked List in JavaScript

A quick introduction to console in JavaScript

console object in browser’s console

there is an interesting sentence what you miss, others will get.

Javascript Core Concepts

Create a Dynamic Headless Blog With Pimcore and Gatsby

Notebook and keyboard on desk

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Tpham

Tpham

More from Medium

All you need to know about var, let & const in JavaScript

JavaScript Closure

JavaScript Fundermentals

What is Callstack 🤔