JavaScript Interview Preparation: let, const, and var

In this article, we’ll dive into the differences between let, const, and var in JavaScript and cover many important problems based on block scope and function scope also explore best practices for their usage.
Read the full article it will definitely help you in your JavaScript interview.
When working with JavaScript, you’ll frequently encounter three keywords for declaring variables: let, const, and var. Each of these has a distinct purpose and scope, and understanding when and how to use them is essential for writing clean and maintainable code.
Points related to var variables:
- Function Scope: Variables declared with
varare function-scoped, which means they are accessible within the function in which they are defined. - Hoisting: Declarations made with
varare hoisted to the top of their containing function or global scope. This means you can use a variable before it's declared, but it will beundefineduntil the declaration is reached in the code. - No Block Scope: Unlike
letandconst, variables declared withvardo not have block scope. They are visible throughout the entire function, even if they are defined within a block (such as anifstatement). - Global Scope: If a
varvariable is declared outside of any function or block, it becomes a property of the global object (e.g.,windowin a browser environment). - Redeclaration: You can redeclare a variable using
varin the same scope without any errors. This can lead to unexpected behavior and bugs. - Considered Outdated: With the introduction of
letandconstin ES6, the use ofvaris generally discouraged in modern JavaScript development due to its scope quirks and potential issues.
Points related to let variables:
- Block Scope: Variables declared with
letare block-scoped, meaning they are only accessible within the block or function where they are defined. - No Redeclaration: Unlike
var, you cannot redeclare a variable usingletin the same scope. Attempting to do so will result in an error. - Temporal Dead Zone (TDZ): Variables declared with
letare hoisted to the top of their containing block or function but remain in an uninitialized state until they are assigned a value. Accessing them before initialization results in aReferenceError. - Global Object Property: Variables declared with
letdo not become properties of the global object, unlikevar, which adds properties to thewindowobject in a browser environment. - For Loops:
letis often used inforloops to create a new variable for each iteration, preventing issues with closures and variable sharing. - Preferred over
var: Due to its block scope and improved scoping behavior,letis generally preferred overvarfor declaring variables in modern JavaScript.
Points related to const variables:
- Constant Value: Variables declared with
constare constants, meaning their values cannot be changed or reassigned after they are initially set. This makesconstsuitable for values that should remain constant throughout your code. - Block Scope: Like
let,constis block-scoped, meaning it is only accessible within the block or function where it is defined. - No Redeclaration: You cannot redeclare a variable using
constin the same scope. Once assigned, aconstvariable's name is locked to the value it holds. - Must Be Initialized: A
constvariable must be initialized (given a value) when it is declared; otherwise, it will result in an error. - Immutable Reference: While the value of a
constvariable is immutable, if it holds a reference to an object (e.g., an array or an object), the properties or elements of that object can still be modified. - Preferred for Constants: Use
constfor values that should not change during the execution of your program, such as mathematical constants or configuration settings. - Consider Using
letorvarfor Mutable Variables: If you expect a variable's value to change, consider usingletorvarinstead ofconst.
Problems Set:
- let and var scope:
// Using 'var' inside a function
function varExample() {
if (true) {
var x = 10; // 'var' is function-scoped
}
console.log(x); // 'x' is accessible outside the if block
}
// Using 'let' inside a block
function letExample() {
if (true) {
let y = 20; // 'let' is block-scoped
}
console.log(y); // 'y' is not accessible outside the if block
}
varExample(); // Outputs: 10
letExample(); // Throws an error: ReferenceError: y is not defined2. Hoisting with var and let:
function hoistingExample() {
console.log(a); // Outputs: undefined
console.log(b); // Throws an error: Cannot access 'b' before initialization
var a = 10; // Variable 'a' is hoisted but initialized with 'undefined'
let b = 20; // Variable 'b' is hoisted but remains uninitialized until this line
}
hoistingExample();3. Pass by value with var and let:
function modify(x, y) {
x = 20; // This does not change the original variable
y = 20; // This does not change the original variable
}
var x = 10;
let y = 10;
modify(x, y);
console.log(x); // Outputs: 10 (unchanged)
console.log(y); // Outputs: 10 (unchanged)4. Pass by reference with var and let:
function modifyArray(arrV, arrL) {
arrV.push(4); // This modifies the original array
arrL.push(4); // This modifies the original array
}
var arrVar = [1, 2, 3];
let arrLet = [1, 2, 3];
modifyArray(arrVar, arrVar);
console.log(arrVar); // Outputs: [1, 2, 3, 4]
console.log(arrLet); // Outputs: [1, 2, 3, 4]5. Redeclaration of the variable with let, const, and var in the same scope:
var x = 10;
var x = 20; // Redeclaring 'x' in the same scope is allowed
console.log(x); // Outputs: 20
let y = 10;
let y = 20; // Throws an error: Identifier 'y' has already been declared
const z = 10;
const z = 20; // Throws an error: Identifier 'z' has already been declared6. Redeclaration of the variable with let, const, and var in the different scope:
var a = 10; // a becomes a property of the global object
if (true) {
var a = 20; // This modifies the original 'a' in the outer scope
}
console.log(a); // Outputs: 20let b = 10; // b do not become properties of the global object,
if (true) {
let b = 20; // This creates a new 'b' in the inner scope
}
console.log(b); // Outputs: 10 (unchanged)const c = 10;
if (true) {
const c = 20; // Throws an error: Identifier 'c' has already been declared
// JavaScript does not allow redeclaration of variables with the const keyword in the same or inner scope.
}7. Use of let and var variables inside the loop:
for (let i = 0; i < 5; i++) { // with let i
setTimeout(function () {
console.log(i); // 0,1,2,3,4
}, 1000);
}
// Explanation: When let is used in the loop, it creates a new variable i
// for each iteration, (have block scope) preserving its value for the setTimeout callback.
// This will correctly log values (0,1,2,3,4) each after a 1-second delay.for (var i = 0; i < 5; i++) {. // with var i
setTimeout(function () {
console.log(i); // 5,5,5,5,5
}, 1000);
}
// Explanation: When var is used, it doesn't create a new variable for each iteration.
// Instead, it declares a single variable i that is function-scoped and shared across all iterations
// As a result, by the time the setTimeout callbacks execute,
// i will be equal to 5 for all of them. So, it will log values 5 five times.Hope you like this article. If you find this article helpful then like it and share it with others.
Thanks for reading.




