JavaScript
  • JavaScript Introduction
  • JS Engine
  • V8 Engine
  • First-class function
  • Optimized Code
  • Call Stack & Memory heap
  • Single Thread
  • JavaScript RunTime
  • Nodejs
  • Context and Environment
  • Hoisting
  • Functions
  • Arguments
  • Variables
  • Scope
  • IIFE
  • this
  • call(), apply() and bind()
  • currying
  • Types
  • Type Coercion
  • Functions as Object
  • HOF (Higher Order Function)
  • Two pillars of Javascript
  • Closures
  • Prototypal Inheritance
  • OOP and FP
  • OOP
    • 4 principles of OOP
  • FP
    • Pure function
    • Imperative vs Declarative
    • Immutability
    • HOF and Closures
    • Currying
    • Partial Application
    • Compose and Pipe
  • Composition vs Inheritance
  • OOP vs FP
  • JS working
  • Promises
  • Async Await
  • ES5 - ECMAScript 2009
  • ES6 - ECMAScript 2015
  • ES7 - ECMAScript 2016
  • ES8 - ECMAScript 2017
  • ES9 - ECMAScript 2018
  • ES10 - ECMAScript 2019
  • ES11 - ECMAScript 2020
  • ES12 - ECMAScript 2021
  • JOB Queue
  • Promises Execution
Powered by GitBook
On this page

Was this helpful?

Hoisting

It is a behaviour of moving variable or function declaration to the top of their environment.

PreviousContext and EnvironmentNextFunctions

Last updated 5 years ago

Was this helpful?

  • JavaScript engine looks for the var and function key as a first word's in the line of the environment, it allocates the memory var and functions in its creation phase.

  • It moves the var and function to the top of the environment on creation phase.

  • The variables will be partially hoisted, that means the variable is created in top as a undefined variable, but functions are fully hoisted, means complete function is moved to the top.

  • EX:

Actual Code

Hoisted code

console.log(text); // undefined

greet(); // hello

console.log(greet2); // undefined

console.log(greet2()); // Type Error: greet2 is not a function

var text = ‘welcome’;

function greet() {

console.log(‘Hello’);

}

var greet2 = function() {

console.log(‘Hello2’);

}

var text = undefined;

var greet2 = undefined;

function greet() {

console.log(‘Hello’);

}

console.log(text); // undefined

greet(); // hello

console.log(greet2) // undefined

console.log(greet2()) // Type Error: greet2 is not a function

var text = ‘welcome’;

var greet2 = function() {

console.log(‘Hello2’);

}

Note: let and const will not be hoisted.

Execution Context