Complete the code to declare a variable using var.
console.log(x); var [1] = 5;
The variable x is declared using var, which is hoisted and initialized to undefined.
Complete the code to show hoisting effect with var.
console.log([1]); var a = 10;
Variable a is hoisted and initialized as undefined before assignment.
Complete the code to log the variable declared with let (this will cause an error).
console.log([1]); let b = 20;
Using let does not hoist the variable; accessing before declaration causes ReferenceError.
Fill both blanks to create a function that logs a hoisted variable.
function test() {
console.log([1]);
var [2] = 30;
}
test();The variable x is hoisted inside the function and logs undefined.
Fill all three blanks to create a block with let and var variables and log the hoisted one.
{
var [1] = 40;
let [2] = 50;
}
console.log([3]);Variable a declared with var is hoisted and accessible outside the block; b declared with let is block-scoped.