DEV Community

CalebMcCoy04
CalebMcCoy04

Posted on

Hoisting

Hello, Just wanted to chat about a topic that confused me in the beginning, hoisting.
Hoisting refers to setting variables, functions, or classes to the top level of their scope.

const hoistedVariable = 1
//this is hoisted 
function something(){
return hoistedVAriable + 1
}
Enter fullscreen mode Exit fullscreen mode

const and let both can be hoisted but are not initialized.

Where as if we had some variable declared within a function it would not be hoisted.

function Something(){
const notHoisted = 2
// not hoisted
return notHoisted + 4
}
Enter fullscreen mode Exit fullscreen mode

the above examples show hoisted vs not hoisted.
there is some interesting stuff with var but for me best practice its not to use var so that wont be covered here.

Top comments (0)