โ—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