Complete the code to access the global variable inside the function.
let message = 'Hello'; function greet() { console.log([1]); } greet();
The variable message is declared globally and can be accessed inside the function greet due to the scope chain.
Complete the code to print the local variable inside the function.
function showNumber() {
let number = 5;
console.log([1]);
}
showNumber();The variable number is declared inside the function and can be accessed there.
Fix the error in accessing the variable inside the nested function.
function outer() {
let outerVar = 'outside';
function inner() {
console.log([1]);
}
inner();
}
outer();The inner function can access outerVar because of the scope chain linking inner and outer functions.
Fill the blank to create a closure that increments the outer variable.
function makeCounter() {
let count = 0;
return function() {
count [1];
return count;
};
}
const counter = makeCounter();
counter();The inner function increments count using ++ and returns the updated value.
Fill all three blanks to correctly shadow a variable and access both values.
let value = 10; function test() { let value = [1]; console.log(value); console.log([2]); function inner() { console.log([3]); } inner(); } test();
The local value shadows the global one. Inside test, value is 20. To access the global value, use globalThis.value. Inside inner, value refers to the local one in test.