3

I expected the answer to be "function text" as output, why the answer is 5?

var alpha = 5;
function alpha(){}
console.log(alpha);

2
  • 2
    functions are hoisted to the top of the scope ... therefore, think of your code as having the function definition first - now it should make sense (why would you want such code!!!) Commented Oct 29, 2020 at 10:29
  • 1
    It is due to hosting in javascript. Regardless of where a function is placed, it is moved to the top of the scope in which it is define. If you want "function alpha" as output, declare function as function expression. Commented Oct 29, 2020 at 10:29

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.