What is hoisting in JavaScript?

Abinash Panda
1 min readDec 31, 2019
  • Hoisting is performed by JavaScript engine its moves the all variable declared by var and function is move to top of of the current scope.
  • variable declared by let and const doesn’t hoisted.

first, scenario

var x = 'YOLO'
console.log(X) // YOLO

second scenario

console.log(x) // undefined
var x = 'YOLO'

it shows undefined beacause var is initalized and given it’s default value undefined.

with function we can call the function before declaration beacause of hoisting.

Ex.

foo()function foo() {
console.log('-_-')
}

but if you use let or const it will give you a error Cannot access ‘x’ before initialization.

console.log(x) // undefined
const x = 'YOLO'

--

--