The next-generation blog, news, and article for you to start sharing your stories today!
Okay, Welcome.
So I am Vikram Parihar, Today we will learn and try to understand the concept of hositing in #javascript. Before we go ahead if you are new to javascript please considered following link to understand the basic functionality and concept of javascript.
In JavaScript, hoisting allows you to use functions and variables before they're declared. In this post, we'll learn what hoisting is and how it works
console.log(hi)
var hi = 'Welcome to hoisting'
Look at the below code, You can see, you are accessing the "hi" variable before your declaration and js interpreter allow you to run this block of code.
This is because the JavaScript interpreter splits the declaration and assignment of functions and variables: it "hoists" your declarations to the top of their containing scope before execution.
When the interpreter hoists a variable declared with
var
, it initializes its value toundefined
. The first line of code below will outputundefined
:
console.log(foo); // undefined
var foo = 'bar';
console.log(foo); // "bar"
let
const
Variables declared with
let
andconst
are hoisted but not initialized with a default value. Accessing alet
orconst
variable before it's declared will result in aReferenceError
:
console.log(foo); // Uncaught ReferenceError: Cannot access 'foo' before initialization
let foo = 'bar'; // Same behavior for variables declared with const
Function hoisting is useful because we can hide function implementation farther down in the file and let the reader focus on what the code is doing. In other words, we can open up a file and see what the code does without first understanding how it's implemented.
Look at the following code
resetScore();
drawGameBoard();
populateGameBoard();
startGame();
function resetScore() {
console.log("Resetting score");
}
function drawGameBoard() {
console.log("Drawing board");
}
function populateGameBoard() {
console.log("Populating board");
}
function startGame() {
console.log("Starting game");
}
However, using functions before their declaration is a matter of personal preference. Some developers, such as Wes Bos, prefer to avoid this and put functions into modules that can be imported as needed (source: Wes Bos).
Airbnb's style guide takes this further and encourages named function expressions over declarations to prevent reference before declaration:
Function declarations are hoisted, which means that it’s easy - too easy - to reference the function before it is defined in the file. This harms readability and maintainability.
If you find that a function’s definition is large or complex enough that it is interfering with understanding the rest of the file, then perhaps it’s time to extract it to its own module! (Source: Airbnb JavaScript Style Guide)
Thanks for reading, and I hope this post helped you learn about hoisting in JavaScript. Feel free to reach out to me on github if you want to connect or have any questions!
Vikram Parihar is an senior software engineer. He is working since 2013 in web development technologies. He works in NodeJS, Angular, Vue, PHP, Laravel, Express JS and various popular technologies.