JS Hoisting

JavaScript hoisting on variable and function

by manoj_antony32

JavaScript

/*------------Variable Declaration--------*/

function test() {
 console.log(x)
}
test();


var a;
var x = 5;
let y = 'hello';

/* below how the code executes line by line */
// 1. First check declarations and its go to top var x;
// 2. And along with that function declarations done
// 3. Finally initialization and function call happens as line by line


/*------------Function Declaration--------*/

test1();
x();

function test1() { // named function will do hoisting
 console.log('Hello');
}

var x = function() { //anonymous function or named function, it wont hoist
 console.log('Hi');
}

//How it works
/* var x;
function test1() {
 console.log('Hello');
}
test1();
x();
x = function() {
 console.log('Hi');
}*/