Javascript Hoisting

Selen Gora
2 min readJul 31, 2019

--

A variable can be used before it has been declared. *

x = 24; // Assign 24 to xconsole.log(x); // 24var x; // Declare x

var, let, const Differences

var and let,const variables are have difference life cycle.

var declaration phase and initialization phase are same level. var variables are hoisted.

let declaration phase after uninitialized state after comes initialization phase.

Hoisting is not valid for a let variable (including for const and class). Before initialization, the variable is in temporal dead zone and is not accessible. *

Little bit deeper on let, const variables, actually they are hoisting but…

It will throw an exception if you access the variable before the initialisation even if the accessing code is below the declaration (e.g. in a hoisted function declaration that is called too early).

user Bergi has the explanation on stackoverflow

Function hoisting?

Function declarations are hoisted

helloFunction(); // Hello hoisting// function declaration
function helloFunction() {
console.log('Hello hoisting');}

Assignment functions(Function expressions) are not hoisted

myNewFunction(); //Uncaught TypeError: myNewFunction is not a function// function expression
let myNewFunction = function(){
console.log('Hello hoisting expression');}

References:

--

--

Selen Gora

Frontend developer who currently works at Tourism Industry.