HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / TypeScript / JavaScript Variable Hoisting

JavaScript Variable Hoisting

Variable hoisting in JavaScript means to move all variable declarations to top of function. This means that if we declare a variable at the end of a function, the runtime will hoist it to the top and we will not have any error if we would have used that variable before being declared.

Please note that variables declared with the var keyword are subject to hoisting. let and const (introduced in ES6) does not have hoisting effect.

What hoisting means?

As said before, hoisting means moving variable declarations to top. It is important to note that hoisting does not happen if you initialize the variable.

Variable declaration (Hoisting happen)

In below code, data is used before it is declared.

function fun() 
{
	data = 1;
	console.log(data);	//Outputs 1
	var data;
}

fun();

At runtime, after hoisting above code will look like this:

function fun() 
{
	var data;	/*** moved to top ***/
	data = 1;
	console.log(data);	//Outputs 1
}

fun();

Variable initialization (Hoisting does NOT happen)

In below code, data is declared as well as initialized also. In this case, hoisting will happen and it will not move upto top. So value of data will be available only after it is declared and initialized; not before.

function fun() 
{
	console.log(data);	//Outputs 'undefined'

	var data = 1;

	console.log(data);	//Outputs 1
}

fun();

Drop me your questions related to variable hoisting in javascript in comments section.

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

TypeScript Tutorial

  • TypeScript – Introduction
  • TypeScript – Types
  • TypeScript – Union Types
  • TypeScript – String Literal Types
  • TypeScript – var, let and const
  • TypeScript – Template Strings
  • TypeScript – Arithmetic Operators
  • TypeScript – Logical Operators
  • TypeScript – Comparison Operators
  • TypeScript – ‘for…of’ Loop
  • TypeScript – Spread Operator
  • TypeScript – Arrays
  • TypeScript – Enums
  • TypeScript – Map
  • TypeScript – Set
  • TypeScript – Functions
  • TypeScript – Function Overloading
  • TypeScript – Transpiler
  • TypeScript – Truthy and falsy
  • TypeScript – == vs ===
  • TypeScript – undefined vs null
  • TypeScript – Variable Hoisting
  • TypeScript – tsconfig.json

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces