WorksheetsJavaScript Variables & Hoisting Quiz
Total questions: 10
Worksheet time: 5mins
Explain the difference between var, let, and const in terms of scope.
let is function-scoped, var and const are block-scoped.
const is function-scoped, var and let are block-scoped.
var is block-scoped, let and const are function-scoped.
var is function-scoped, let and const are block-scoped. const variables cannot be reassigned.
Consider this code snippet:
console.log(a); // What will be the output?
var a = 10; console.log(a); // What will be the output?
What is hoisting in JavaScript?
Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope before code execution.
Hoisting is a JavaScript concept that refers to the process of optimizing code execution for faster performance.
Hoisting is a JavaScript feature that allows variables to be declared without using the 'var' keyword.
Hoisting is a JavaScript mechanism where variables and function declarations are moved to the bottom of their scope before code execution.
Consider this code snippet:
{
let d = 25;
}
console.log(d); // What will be the output?
ReferenceError
When should you use const over let in JavaScript?
When the variable is declared inside a function
When the variable will be reassigned multiple times
When the variable is used in a loop
When the variable will not be reassigned
What is the difference between global and local scope in JavaScript?
Global scope variables are only accessible within the function they are declared in.
Global scope variables are accessible from anywhere in the code, while local scope variables are only accessible within the function they are declared in.
Local scope variables are accessible from anywhere in the code.
Global scope variables are limited to a specific block of code.
What type of error happens if you try to access a variable before it is declared in JavaScript?
SyntaxError
TypeError
RuntimeError
ReferenceError
Consider this code snippet:
console.log(b); // What will be the output?
let b = 15;
console.log(b); // What will be the output?
The output will be: ReferenceError, 15
Consider this code snippet:
{
var c = 20;
}
console.log(c); // What will be the output?
ReferenceError
How does the use of var, let, and const impact memory management in JavaScript?
Var, let, and const in JavaScript determine the order of execution of variables in memory.
The use of var, let, and const impacts memory management in JavaScript by defining the color of variables in memory.
The use of var, let, and const impacts memory management in JavaScript by defining how variables are stored in memory. 'var' variables are function-scoped and can be redeclared and updated, potentially leading to memory leaks. 'let' variables are block-scoped and can be updated but not redeclared, providing better memory management. 'const' variables are also block-scoped but cannot be reassigned, ensuring memory integrity.
Using var, let, and const in JavaScript has no impact on memory management.
