#82: A function and a var sharing a name
Topic: hoisting · Difficulty: ★★★
console.log(typeof greet);
var greet = 'hi';
function greet() {}
console.log(typeof greet);Output
function string
Why
Both var greet and function greet() get hoisted, but hoisting isn't first-come-first-served: function declarations are hoisted WITH their value already attached (a callable function), while var greet is hoisted as a bare, uninitialized name that only gets its value when the assignment line actually executes. Since the function's full hoisting effectively happens 'after' — and does not get overwritten by an empty var hoist — typeof greet at the very top is 'function', not 'undefined'. Only once the line `var greet = 'hi'` runs does greet get overwritten with the string, so the second check reports 'string'.