Hoisting in JavaScript?
Table of contents
What is Hoisting?
Hoisting is JavaScript's default behavior of moving all declarations to the top of the current scope (to the top of the current script or the current function). This means that you can use a variable or a function before it has been declared in your code, without getting an error.
Hoisting is JavaScript default behavior that allows you to define a function at the bottom of your file yet still use it at the top of the file before it is defined.
// This is normal function and hoisting will work for this function
console.log(add(5,5) // 10
function add(a,b){
return a+b;
}
// This is function expression and hosting will not work here but will
//give error
console.log(subtract(5,5))
let subtract = function(a,b){
return a-b;
}
Note: JavaScript only hoists function not the function expression