4
const func = () => {
  someFunction() // error here
  
  return function someFunction() {
    console.log('hello')
  }
}

func()

I've created closure and wanted to check hoisting inside of func function. Each time when you create function declaration it hoists your variable up to the top. Why is someFunction not hoisted?

halfer
  • 18,701
  • 13
  • 79
  • 158
Andrey Radkevich
  • 1,127
  • 1
  • 9
  • 23
  • Does this answer your question? [var functionName = function() {} vs function functionName() {}](https://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname) – VLAZ Oct 02 '20 at 16:19
  • See here: https://blog.logrocket.com/demystifying-function-and-variable-hoisting-in-javascript/ – AbsoluteBeginner Oct 02 '20 at 16:19

2 Answers2

8

When you put a function after return statement, it no longer is a function declaration, but rather a function expression. Unlike declarations, function expressions are not hoisted.

Function expressions in JavaScript are not hoisted, unlike function declarations.

- MDN

Nick Parsons
  • 31,322
  • 6
  • 25
  • 44
6

You have a (named) function expression in your return statement. This function is not a function statement and because of this, it is not hoisted.

Another reason is, a function expression has no name. That means, you can not access it by the a name outside of the function. The name of a function expression is only available inside of the function with it name (for example for recursions).

Nina Scholz
  • 323,592
  • 20
  • 270
  • 324