Complete the code to declare a block-scoped variable.
function test() {
[1] message = 'Hello';
console.log(message);
}The let keyword declares a block-scoped variable inside the function.
Complete the code to correctly access the block-scoped variable inside the block.
if (true) { let [1] = 5; console.log(num); }
The variable declared with let is named num and accessible inside the block.
Fix the error by choosing the correct keyword to declare a block-scoped constant.
if (true) { [1] PI = 3.14; console.log(PI); }
const declares a block-scoped constant that cannot be reassigned.
Fill both blanks to create a block-scoped variable and check its scope.
function checkScope() {
[1] x = 10;
if (true) {
[2] x = 20;
console.log(x);
}
console.log(x);
}var for both variables causes the inner assignment to overwrite the outer.const incorrectly when reassignment is needed.The outer x is declared with var (function-scoped), and the inner x with let (block-scoped). The inner x shadows the outer inside the block.
Fill all three blanks to create a block-scoped variable, a constant, and print their values.
function demo() {
[1] name = 'Alice';
if (true) {
[2] greeting = 'Hello';
[3](greeting + ', ' + name);
}
}const for variables that are reassigned.console.log to print.name is declared with var (function-scoped), greeting with let (block-scoped), and console.log prints the combined message.