Complete the code to declare a variable inside the function scope.
function greet() {
let [1] = 'Hello';
console.log(greeting);
}
greet();The variable greeting is declared inside the function using let. This means it is only accessible within the function.
Complete the code to access a variable declared outside the function.
let name = 'Alice'; function sayName() { console.log([1]); } sayName();
The function can access the variable name declared outside because of JavaScript's scope chain.
Fix the error in the code by completing the blank with the correct variable name.
function test() {
let score = 10;
if (true) {
let score = 20;
console.log([1]);
}
console.log(score);
}
test();Both score variables are valid but in different scopes. Inside the if-block, score refers to 20, outside it refers to 10.
Fill both blanks to create a function that returns the sum of two numbers.
function add([1], [2]) { return a + b; } console.log(add(3, 4));
The function parameters must be a and b to match the return statement a + b.
Fill all three blanks to create a function that updates a variable inside its scope and logs it.
function update() {
let count = 0;
count [1] 5;
console.log([2]);
return [3];
}
update();-=.The operator += adds 5 to count. Then count is logged and returned.