0

According to hoisting definition:

Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope before code execution

Why do function declarations get hoisted and function expressions don't?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Priyank
  • 3,778
  • 3
  • 29
  • 48
  • 1
    In simple terms, named variables are hioisted. In function expression, variable name is hoisted but the actual function is a value that is assigned later – Rajesh Sep 05 '19 at 06:30
  • 1
    Function expressions aren't added to the scope at all, hoisted or otherwise. This is because they are being used as a _value_, as opposed to function declarations, whose purpose is to create functions you can call by name. – JLRishe Sep 05 '19 at 06:33
  • 2
    Because "hoisting" does not exist. It is just a theory invented to give a simple explanation of how javascript works but it is not actually how javascript works. See my answer to this other question for an explanation of what's happening: https://stackoverflow.com/questions/3887408/javascript-function-declaration-and-evaluation-order/3887590#3887590 – slebetman Sep 05 '19 at 06:35
  • 1
    I would imagine the question is being downvoted because it's not actually asking for help. "Why was [language X] designed this way?" questions are not very good questions for SO because they do not actually seek to solve a technical problem, and can be extremely hard to answer because it requires knowledge of why the language designers made the decisions they made. – JLRishe Sep 05 '19 at 06:37
  • @JLRishe I agree with your 2nd point i.e. "hard to answer because it requires knowledge of why the language designers made the decisions they made." but for the 1st point i.e. seek to help is valid as I want to understand this concept and "slebetman" explain very well which is helpful for me. thanks!! upvote my ques if it still make sense for you guys – Priyank Sep 05 '19 at 06:43
  • 1
    @Priyank If you are asking for the actual, technical reasons (e.g. in terms of the spec) why it acts this way, then that's another matter. I'd suggest clarifying your question to indicate that that's what you're asking about. However, I suspect that that question has probably been answered before. – JLRishe Sep 05 '19 at 06:52
  • 1
    @Priyank not the voter but ini my understanding, downvotes are because this is something that would have been resolved if you had referred the docs. I understand its sometimes difficult but if you would try to formulate question like *Why something... I have tried/ search this and that. Could you help/ clarify my understanding/ assumption?* – Rajesh Sep 05 '19 at 07:11

1 Answers1

2

As per MDN,

Conceptually, for example, a strict definition of hoisting suggests that variable and function declarations are physically moved to the top of your code, but this is not in fact what happens. Instead, the variable and function declarations are put into memory during the compile phase, but stay exactly where you typed them in your code.

As you see, in a function expression, actual function is a value assigned to a named variable. So this named variable is hoisted. Even if you have a named function assigned, it still will not be hoisted as it is not a declaration and will be created later.

Sample:

function test() {
  console.log(fn, foo);
  
  var fn = function foo() {}
}

test();
Rajesh
  • 24,354
  • 5
  • 48
  • 79