I have the below code, and it prints 42.
{
let number = 42;
function printNumber() {
console.log(number);
}
function log() {
let number = 54;
printNumber();
}
// Prints 42
log();
}
This output brought me to wonder the function call is replaced by function definition or not?
I tried removing the "number" variable declaration in the beginning of the program to see if it reads the "number" defined inside the log() function but it returned reference error,
{
//let number = 42;
function printNumber() {
console.log(number);
}
function log() {
let number = 54;
printNumber();
}
// Uncaught ReferenceError: number is not defined
log();
}
I want to understand how the function call is interpreted and how the scope is determined that if a function printNumber() is called inside another function log() then how is it not supposed to use the variable declared inside the function log() from where it is called.