function expression not hoisted

a function expression is never hoisted by itself

👉 compare: function hoisting, var hoisting.

this is actually an example of var hoisting, about the hoisting of a var (whose value happens to be a function expression).

const { log } = console;

// ⭐️ var hoisting
// ----------------
// • identifier: IS hoisted❗
// • value     : initialized to `undefined`❗
log(greeting, typeof greeting);        // undefined, 'undefined'

// ⭐️ identifier `greeting`:
// • can be referenced now❗ (not a compile-time ReferenceError)
// • but can't be called (not a "function") yet❗(runtime ⛔ TypeError)
greeting();            // ⛔ TypeError: 'greeting' is not a function

// ⭐️ the following is a "var declaration"
// ⭐️ "var declaration" is related to "var hoisting"
// ------------------------------------------------------
// • identifier: hoisted to top of scope❗
// • value     : initialized to `undefined`❗
// ------------------------------------------------------

// ⭐️ "var declaration"
// 
//  ╭─ id ─╮   ╭── value ──╮ (`value` is a function expression)
var greeting = function() {
    console.log("Hello!");
};

Last updated