r/learnjavascript • • Jul 19 '21

JavaScript Function Facts

255 Upvotes

27 comments sorted by

View all comments

20

u/[deleted] Jul 19 '21

function declarations aren't the only thing that is hoisted, variable declarations with var are also hoisted.

e.g. the following does not work:

console.log('hello', future());
const future = function() { 
   return ' world'; 
}

3

u/Claudioub16 Jul 19 '21

I'm new to programming, but I really dislike hoisting. Why would you want to hoist a function?

1

u/Trakkasure helpful Jul 20 '21

Hoisting functions allow for functions that call each other to do so without procedural gymnastics.

function a() { if (repeat) c() }
function b() { a() }
function c() { b() }