TechMentorAI

Hoisting

in JavaScript

Published

In JavaScript, when you create a variable, you're telling the computer to set aside some memory to store a value that you can use later. Normally, you'd expect that until you reach the line of code where you declare your variable, the computer doesn't know it exists. But JavaScript does something clever called 'Hoisting'.

Imagine Hoisting in JavaScript

What is Hoisting?

In JavaScript, before the code is executed, the computer makes two passes over your code. In the first pass, it looks for any variables and function declarations and moves those to the top. This process is known as 'Hoisting'. The second pass executes your code. Because of this, you can use variables and functions in your code before they're declared, because the declarations get 'hoisted' to the top.

Example of Hoisting

Here is an example of hoisting. Normally, we would think that trying to print the variable before it's declared would give us an error. But thanks to hoisting, it doesn't.

console.log(myVar); // Output: undefined

var myVar = 5; // Declare and Initialize after usage

console.log(myVar); // Output: 5

A Caution about Hoisting

It's important to note that while the declarations are hoisted, assignments are not. In the previous example, the declaration 'var myVar;' was hoisted, but not the assignment 'myVar = 5;'. This is why the first console.log printed 'undefined' instead of '5'.

No Time to Read? Learn on the Go!

By reading this article, you've invested 1.04 minutes of your life into expanding your knowledge and perspectives. Now, imagine learning on-the-go, turning every moment into an opportunity for growth and discovery.

Imagine you're playing a game of hide and seek. Before the game starts, you already know the best hiding spots because your big brother told you so. Although you haven't physically checked them yet, you already know they exist and where they are. In JavaScript, hoisting is a bit like knowing the hiding spots before the game starts. This means that you can use things like variables and functions even before you say what they are!

Imagine Hoisting in JavaScript

Hoisting with Variables

When you say you have a box named 'toys' but you don't yet show or say what's inside the box, JavaScript knows the box named 'toys' exist but doesn't know what's in the box until you show it. This is how variables are hoisted in JavaScript.

console.log(toys);
var toys = 'teddyBear';
// Output: undefined

Hoisting with Functions

Now imagine you have a magic trick that you're going to perform later. Everyone knows you have a magic trick, even before you perform it. In JavaScript, functions work similarly. They can be used before they are declared or defined in the code.

magicTrick();
function magicTrick() {
 console.log('Ta-da! Magic!');
}
// Output: 'Ta-da! Magic!'

Let and Const

In JavaScript, if we use 'let' or 'const' instead of 'var' to say we have a box, JavaScript won't recognize the box until we actually show it. That's why it's considered a better practice to use 'let' and 'const' instead of 'var'.

console.log(toys);
let toys = 'teddyBear';
// Output: Error! toys is not defined```

```javascript
console.log(toys);
const toys = 'teddyBear';
// Output: Error! toys is not defined

Summary

So, in a nutshell, hoisting in JavaScript is like knowing the best hiding spots before your game of hide and seek starts or knowing about your magic trick before you even perform it. But remember, with 'let' and 'const', you need to show your box or magic trick before you can talk about it!

Understanding More

If you want to understand more about how JavaScript works, you should read about how JavaScript reads code. It's like the rules of the hide and seek game. Understanding the rules will make you a better player!

No Time to Read? Learn on the Go!

By reading this article, you've invested 1.56 minutes of your life into expanding your knowledge and perspectives. Now, imagine learning on-the-go, turning every moment into an opportunity for growth and discovery.

If you want to further increase your knowledge about JavaScript concepts, you might want to check out closures in JavaScript, another important concept, here: Understanding Closures in JavaScript

About author

Roman Y.
Senior Software Engineer at Nike

Why did I decide to launch this website? Drawing from my rich background in interviewing candidates for a variety of developer roles, I've observed a common pattern: many applicants share similar deficiencies in their knowledge during technical interviews. Motivated by this insight, I established this website with the aim of assisting developers in securing their ideal job. Through straightforward and concise articles, my goal is to not only deepen your understanding of programming language nuances but also to equip you with the insights needed to deliver the precise answers interviewers expect. Essentially, you'll be providing the correct response. I encourage you to spread the word about this site on social media platforms. Echoing the wisdom of an Armenian saying: "Do good and cast it into the water."

EXCLUSIVELY

Certain articles only distributed to subscribers through email