I am going through Head First JavaScript to understand nested functions. Below is the code I am failing to understand.
var migrating = true;
if (migrating) {
quack(4);
fly(4);
}
var fly = function(num) {
var sound = "Flying";
for (var i = 0; i < num; i++) {
wingFlapper();
}
function wingFlapper() {
console.log(sound);
}
};
function quack(num) {
var sound = "Quack";
for (var i = 0; i < num; i++) {
quacker();
}
var quacker = function() {
console.log(sound);
};
}
The book says "quacker is defined by a function expression in the function quack. So its scope is the entire quack function but it’s defined only after the function expression is evaluated, until the end of the function body." So I am calling the function before its evaluated so shouldn't it throw an exception or I am not understanding the concept?