Understanding Hoisting in JavaScript for Beginners

Understanding  Hoisting in JavaScript  for Beginners

In this post, we are going to discuss hoisting. We’ll be discussing what it is and why it’s important for you to know as a Javascript . Hoisting is JavaScript's default behavior of moving declarations to the top. ... To understand this, you have to understand the term "hoisting". Hoisting is ..

In this post, we are going to discuss hoisting. We’ll be discussing what it is and why it’s important for you to know as a Javascriptdeveloper.

This article is the first of many small articles discussing key areas and terms in Javascript, such as call stack, callback, promise, async, etc.


You may also like: Three Ways to Define Functions in JavaScript


Hoisting

Before we start discussing what hoisting is, let us first look at a simple example:

var myName = 'Aditya Nagar'

console.log(myName)
// Aditya Nagar

This is pretty straightforward — I declared a variable, initialized some value to it, and then logged it in the console.

Now, let's look at the definition of hoisting_:_

Simply put, it means no matter where you declare functions or variables when the code is executed, they’re moved to the top of their scope.

According to the above statement, if I were to move my variable declaration below the console log statement, it should work just fine. But that’s not the scenario. Let’s see why that is.

console.log(myName)
//undefined
var myName='Aditya Nagar'

Now, the question is, "Why am I getting undefined when I should be getting the myName variable? Is hoisting not working?"

The simple answer is that hoisting is working just fine. If you go back to the definition of hoisting, it specifically says that only the declaration is moved to the top and not the initialization.

So in the above example, our variable declaration is moved to the top, while it’s initialized value is not. The reason why we are getting undefined is because it’s the default value of the declared variable in JavaScript.

If hoisting was not working, then we would have got an error that the myName variable is not defined.

So, because of hoisting, we can use the variables no matter where they are declared in our code. However, using the value is a different scenario.

Hoisting in Function Declaration

myName()
//'My name is Vishu Saanp'
function myName(){
console.log('My name is Vishu Saanp')
}

// Function Declaration is moved to top along with function body

Here, I called the function before I even declared it — this is hoisting in the function declaration.

Hoisting in Function Expression

But this is not the case in a function expression. Let’s look at why.

sayHello()
//error: Uncaught TypeError: sayHello is not a function
var sayHello= function(){
console.log('Hello, how are you?')
}

So, why is it working in the function declaration but not in function expression?

Well, it’s because a function expression is a function that is assigned to a variable, and, in this case, the variable declaration is moved to the top with the default value of undefined rather than the function.

Conclusion

With this, we have successfully covered hoisting, and if you have any questions, feel free to ask in the comments. We will be discussing call stack in the next post.

Further Reading

Understanding the Spread Operator in JavaScript

New ES2019 Features Every JavaScript Developer Should Know

JavaScript array methods you should know

javascript web-development

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire Web Developer

Looking for an attractive & user-friendly web developer? HourlyDeveloper.io, a leading web, and mobile app development company, offers web developers for hire through flexible engagement models. You can **[Hire Web...

Why Web Development is Important for your Business

With the rapid development in technology, the old ways to do business have changed completely. A lot more advanced and developed ways are ...

Important Reasons to Hire a Professional Web Development Company

    You name the business and I will tell you how web development can help you promote your business. If it is a startup or you seeking some...

Hire Dedicated eCommerce Web Developers | Top eCommerce Web Designers

Build your eCommerce project by hiring our expert eCommerce Website developers. Our Dedicated Web Designers develop powerful & robust website in a short span of time.

How long does it take to develop/build an app?

This article covers A-Z about the mobile and web app development process and answers your question on how long does it take to develop/build an app.