const globalVar = "I am global";
function exampleFunction() {
console.log(globalVar); // Accessible here
}
console.log(globalVar); // Accessible here too
// module.js
const moduleVar = "I belong to this module";
// main.js
console.log(moduleVar); // Will result in an error, moduleVar is not defined here
function exampleFunction() {
const functionVar = "I'm scoped within this function";
console.log(functionVar); // Accessible here
}
console.log(functionVar); // Will result in an error, functionVar is not defined here
if (true) {
let blockVar = "I'm scoped within this block";
console.log(blockVar); // Accessible here
}
console.log(blockVar); // Will result in an error, blockVar is not defined here
{
let blockScopedVar = "I'm scoped within this block";
console.log(blockScopedVar); // Accessible here
}
console.log(blockScopedVar); // Will result in an error, blockScopedVar is not defined here
const globalVar = "I'm accessible everywhere";
function exampleFunction() {
console.log(globalVar); // Accessible here
}
console.log(globalVar); // Accessible here too