Skip to content

Day 3: Scope & Default Parameters

Understand variable accessibility across scopes and assign fallback default parameters.

const globalVar = 'Accessible everywhere';
function demoScope() {
const functionVar = 'Only inside demoScope';
if (true) {
const blockVar = 'Only inside this if block';
console.log(blockVar);
}
// console.log(blockVar); // ReferenceError!
}
function greetUser(name = 'Guest', greeting = 'Welcome') {
return `${greeting}, ${name}!`;
}
console.log(greetUser()); // "Welcome, Guest!"
console.log(greetUser('Alice')); // "Welcome, Alice!"
  1. Create a function with 2 default parameters and invoke it with 0, 1, and 2 arguments.
  2. Intentionally try reading a block-scoped variable outside its curly braces.