The problem of function declaration

in variable declaration

    var fn = function fn2() {}
    
    console.log(fn) // function
    console.log(fn2) // Uncaught ReferenceError: fn2 is not defined

Why is the fn2 here not defined?? Is it because it"s in fn2"s own scope?

Mar.30,2021

fn2 can only be referenced in fn2


function fun2 () {}
var fn = fn2
console.log (fn)
console.log (fn2)


javascript the definitive guide 8.1

An identifier that names the function. The name is a required part of function declaration statements: it is used as the name of a variable, and the newly defined function object is assigned to the variable. For function definition expressions, the name is optional: if present, the name refers to the function object only within the body of the function itself.

that is, the identifier (function name) in the assignment function definition is optional, and even if provided, it can only be referenced in the function body;

Menu