DEV Community

Discussion on: Named Function vs. Variable Function?

Collapse
 
rdutta845 profile image
Rahul Dutta

hoisting will happen only for named function, not for variable function

Collapse
 
aaron_powell profile image
Aaron Powell

Hey Rahul, I mentioned that in the post but what does this actually help with? I understand hoisting (I believe) but surely decently written code wouldn't rely on hoisting anyways?

Collapse
 
ahferroin7 profile image
Austin S. Hemmelgarn

Most code doesn't need to rely on it, but it's not unusual to see code that does.

The simplest example of it's usefulness is that it lets you sort your functions by name instead of by what each of them depends on, which is helpful for finding code quickly.

It also lets you do cyclical recursion (that is, two or more functions that from a recursive loop), which is impossible without some form of hoisting, forward-declaration, or multi-pass parsing.