3

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

var alpha = 5;
function alpha(){}
console.log(alpha);
Deadpool
  • 7,811
  • 9
  • 44
  • 88
  • 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!!!) – Jaromanda X Oct 29 '20 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. – Vimal Patel Oct 29 '20 at 10:29

0 Answers0