Member-only story

Hoisting in JavaScript

Mastering JavaScript: Deep Dive Into Hoisting

Patrick Karsh
3 min readDec 20, 2023

Hoisting in JavaScript is a concept that refers to the behavior of variable and function declarations in your code. Understanding hoisting is crucial for developers working with JavaScript, as it affects the accessibility of functions and variables throughout the code.

Lets get this to the top of that scope

What is Hoisting?

In JavaScript, hoisting is the default behavior of moving all declarations to the top of the current scope (the script or the function) during the compilation phase. This means that variables and functions are processed before any code is executed. However, it’s important to note that only the declarations are hoisted, not the initializations.

Variable Hoisting

When a variable is declared using var, its declaration is hoisted to the top of its current scope. If it's declared outside of any function, it is hoisted to the top of the global scope.

In the example above, the declaration of x (var x) is hoisted to the top of its scope. Therefore, x is undefined when it's first logged, as only the declaration, not the initialization, is…

--

--

Patrick Karsh
Patrick Karsh

Written by Patrick Karsh

NYC-based Ruby on Rails and Javascript Engineer leveraging AI to explore Engineering. https://linktr.ee/patrickkarsh

No responses yet