1

I was just trying function hoisting in js , I have a doubt in below code

// Case 1
var foo = function() {
    return 3;
}

// Case 2
function foo() {
    return 2;    
}
alert(foo());

Why case1 gets preference over case 2 always? case 2 never overrides case 1 , why?

4
  • 1
    stackoverflow.com/questions/336859/… Commented Sep 22, 2014 at 15:09
  • In function expression(case 1): only the variable foo is hoisted. Not the function body. In function statement(case 2): the entire function foo is hoisted to the top Commented Sep 22, 2014 at 15:11
  • 1
    I don't think the "Usual" functions link answers this question. The question is specifically about "hoisting," which @Shibbir explains in his comment. Commented Sep 22, 2014 at 15:14
  • @epascarello's link is a better reason for closing the question. Commented Sep 22, 2014 at 15:16

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.