Do you really know about Hoisting?

function hoist() {
a = 20;
var b = 100;
}

console.log(a); // error a is not defined

hoist();

console.log(a); // 20 a becomes global variable
console.log(b); // error b is not defined (it is defined in hoist method scope not global)

Difference between var, let and const in terms of hoisting

console.log(x); // undefined
var x = 10;
var x;
console.log(x);
x = 10;
function getName() {
console.log(name);
var name = "Max";
}

getName(); // undefined
function getName() {
var name = "Max";
console.log(name);
}

getName(); // Max
console.log(age); // Output: ReferenceError: age is not defined ...
let age = 25;
let age;
console.log(age); // undefined
age = 25;
console.log(age); // Output: ReferenceError: age is not defined ...
const age = 25;
const age;  // Missing initializer in const declaration

Hoisting Functions

Function declaration

function print(var name) {
console.log(name);
}
print("Alax");  // Alax

function print(var name) {
console.log(name);
}

Functional expression

let printFn = function(var name) {
console.log(name);
}
printFn();   //Output: "printFn: expression is not a function

let printFn = function(var name) {
console.log(name);
}

Order of precedence

var name = "Tom";

function name() {
console.log("Harry");
}

console.log(typeof name); // string (variable assignment take precedence)
// 2nd Examplevar name;

function name() {
console.log("Harry");
}

console.log(typeof name); // function (function declaration take precedence)

Hoisting Classes

Class declarations

var name = fullName();
name.firstName = "Sourav";
name.lastName = "Ganguly";
console.log(name); // Reference error!!!

class fullName {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
}
class fullName {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
}

var name = fullName();
name.firstName = "Sourav";
name.lastName = "Ganguly";
console.log(name); // Output {firstName: "Sourav", lastName: "Ganguly"}

ClassExpression

var name = getFullName();
name.firstName = "Sourav";
name.lastName = "Ganguly";
console.log(name); // TypeError!!! getFullName is not a constructor

var getFullName = class fullName {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
};

--

--

--

Full stack developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Profiling — React Applications

What is Singleton pattern and what is the advantage of it.

How JavaScript works: the different types of conditional statements + 3 best practices

Making Use of Postman Variables

Building your vCard using Bootstrap 4

Install Express, set up Server & API

Day 14: Tracking user location on embedded Google Maps

Creating a Javascript GitHub Action to Notify Slack

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Rohan Aggarwal

Rohan Aggarwal

Full stack developer

More from Medium

required() in JS

Lets get user inputs using inquirer and Yargs — Creating a npm command line Utility — Part 2

Operators, Scope, and Functions.

TypeScript 4  — Introduction