0

There is this answer on Stack Overflow to support that JS function declarations hoist before variable declarations. It's based on very earlier version of Spec.

Link to the answer - Order of hoisting in JavaScript

Link to the spec it refers - https://www.ecma-international.org/ecma-262/5.1/#sec-10.5

The reasoning is based on step 8(c) of sec 10.5

Now as per the modern spec, can someone tell me that where this order of hoisting is discussed or is it still relevant to say that function declarations are hoisted before variable declarations ?

Link to the current spec - https://www.ecma-international.org/ecma-262/11.0/index.html

Lakshya Thakur
  • 8,030
  • 1
  • 12
  • 39
  • From an answer on the linked topic: "*that order really is not that important.*" And this has not changed. – Bergi Sep 06 '20 at 14:48
  • Uh, you've linked the spec from two years ago, not the current one :-) But the section where "hoisting" is described is still [*FunctionDeclarationInstantiation*](https://www.ecma-international.org/ecma-262/11.0/index.html#sec-functiondeclarationinstantiation). – Bergi Sep 06 '20 at 14:53
  • Btw https://tc39.es/ecma262/ is the current draft of the *next* spec, not the current spec. I would avoid linking it as it changes fairly often. – Bergi Sep 06 '20 at 14:54
  • @Bergi Ahh okay. Can you specifically tell me which section of FunctionDeclarationInstantiation states the order as such? I am sorry I am not a spec reader but today after going through multiple questions, I felt like digging a bit deeper. – Lakshya Thakur Sep 06 '20 at 14:56
  • The relevant steps would be 10, 14, 27, 36. Yes, this spec section has changed considerably since ES5.1, and it doesn't even work in the same order any more - but the result is still the same: declared names with a function get initialised to that function, declared names without get initialised to `undefined`. It is not observable to anything in what order the creation of bindings happens. – Bergi Sep 06 '20 at 15:08

1 Answers1

0

I think the debate of hoisting order of function dec vs variable dec is not much relevant. Function decs are fully present in memory during creation phase. Only when a var declaration with same name as function is assigned a value , the function declaration is no more valid. But if there is just a var declaration with same name as function, it is ineffective. (This was actually a part of earlier spec) As per modern spec, it seems the order doesn’t matter since the declarations never actually move up. It’s actually those being in memory before execution phase. That’s why var coming before function or function coming before var declaration wouldn’t make a difference.

mridul
  • 21
  • 1
  • 4
  • "*Function decs are fully present in memory during creation phase*" - not exactly, function declarations are getting *created* during "creation phase". – Bergi Sep 06 '20 at 16:09
  • Hey @Bergi you can ignore this answer (this was shared by me in our js discussion group. Someone copy pasted it here for fun ) . And yea you are right, bit of a typo there . By the end of creation phase, function declarations are created in memory. – Lakshya Thakur Sep 06 '20 at 16:19