Scope question

Hi ! I’m struggling with Javascript right now and I have a question : In the following exercise , shouldn’t the message error appear since the moment I call the function ( because the function uses the variable) ? Why does it appear only with the concole.log? Thks !

const logVisibleLightWaves = () => {
const lightWaves = ‘Moonlight’;
console.log(lightWaves);
};

logVisibleLightWaves();

console.log(lightWaves);

Indeed. This is the error message that I got when running the code in an online IDE.

node /tmp/8UhQBACDlq.js
ERROR!
Moonlight
/tmp/8UhQBACDlq.js:8
console.log(lightWaves);
            ^

ReferenceError: lightWaves is not defined
    at Object.<anonymous> (/tmp/8UhQBACDlq.js:8:13)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:86:12)
    at node:internal/main/run_main_module:23:47

Node.js v18.18.0

This is because the variable lightWaves is defined in function logVisibleLightWaves, so it is considered as BLOCK SCOPE (function scope). We cannot access BLOCK SCOPE values from outside the function. If we declare and assign another variable (with the same name) outside the function with some value, we will see different outputs when displaying both variable names.

const logVisibleLightWaves = () => {
const lightWaves = ‘Moonlight’;
console.log(lightWaves);
};

logVisibleLightWaves();

console.log(lightWaves); // undefined
lightWaves = "Light of Oreo"
console.log(lightWaves) // Light of Oreo