Javascript has this funny thing called function hoisting, which means we can use a function expression before (that is, visually) it is declared (caution: my wording may be incorrect, feel free to comment on it).
fn1(); // works OK
function fn1() {console.log('fn1');}
However, if we pass a named function as an argument, function hoisting does not happen. The syntax seems very similar but I suppose this is not a function expression anymore but a named anonymous function (!).
fn2(); // throws a ReferenceError as fn2 is not defined
setTimeout(function fn2() {console.log('fn2')}, 0);
- Is it indeed a difference between a function expression and a named lambda ?
- Are there other notable differences between the two ?
- Considering fn2 is a named lambda, is the
fn2
name accessible from anywhere else than in fn2 itself ?