J

JavaScript Handbook

Clean • Professional

JavaScript Variables & Constants — Top Interview Q&A

3 minute

JavaScript Variables & Constants — Interview Questions & Answers

Ques: What is a variable in JavaScript?

Ans: A variable in JavaScript is a container for storing data values such as numbers, strings, or objects.

Example:

let name = "Aman";
let age = 25;
console.log(name, age);

Ques: What are the different ways to declare variables in JavaScript?

Ans: There are three keywords for declaring variables:

  • var — function-scoped and hoisted
  • let — block-scoped, introduced in ES6
  • const — block-scoped, cannot be reassigned

Ques: What is the difference between var, let, and const?

Difference Between var, let, and const

Featurevarletconst
ScopeFunctionBlockBlock
Re-declarationAllowedNot allowedNot allowed
Re-assignmentAllowedAllowedNot allowed
HoistingYes (undefined)Temporal Dead ZoneTemporal Dead Zone

Ques: What is Scope in JavaScript?

Ans: Scope defines where a variable is accessible — global, function, or block scope.

Variables declared with let and const are block-scoped, while var is function-scoped.

Example:

{
  let x = 10;
  var y = 20;
}
console.log(y); // Works
console.log(x); // Error

Ques: What is Global Scope vs Local Scope?

  • Global Scope → Accessible everywhere.
  • Local Scope → Accessible only within function/block.
let globalVar = "I’m Global";

function demo() {
  let localVar = "I’m Local";
  console.log(globalVar); // 
  console.log(localVar);  // 
}

console.log(globalVar); // 
// console.log(localVar); //  Error

Ques: What is Block Scope?

Ans: Variables declared with let and const inside {} exist only within that block.

{
  let name = "Aman";
  const age = 25;
}
// console.log(name); //  ReferenceError

Ques: What is Hoisting in JavaScript?

Ans: Hoisting means variable and function declarations are moved to the top of their scope before execution. However, only var is initialized with undefined; let and const are not accessible until declared (Temporal Dead Zone).

console.log(a); // undefined (due to hoisting)
var a = 10;

Ques: What is the Temporal Dead Zone (TDZ)?

Ans: The TDZ is the time between hoisting and actual declaration of let and const variables, during which they cannot be accessed.

console.log(num); //  ReferenceError
let num = 5;

Ques: Can constants hold objects or arrays?

Ans: Yes, but the reference cannot change — only the values inside can.

const user = { name: "Aman" };
user.name = "Ravi"; //  allowed
// user = {}; //  not allowed

Ques: What are uninitialized variables in JavaScript?

Ans: Variables declared but not assigned a value automatically get the value undefined.

let data;
console.log(data); // undefined

Ques: Why should we avoid using var in modern JS?

Because:

  • It ignores block scope.
  • Causes unexpected bugs due to hoisting.
  • let and const provide safer, cleaner scoping.

Ques: How does JavaScript store variables in memory?

  • Primitive types (string, number, boolean, etc.) are stored by value.
  • Non-primitive types (object, array) are stored by reference.
let a = 5;
let b = a;
b = 10;  // doesn't affect a

let obj1 = { name: "Aman" };
let obj2 = obj1;
obj2.name = "Ravi"; // affects obj1 too

Ques: What are some common mistakes with variables in JS?

  • Using var inside loops or conditions (scope leaks)
  • Forgetting to declare (x = 10; → creates global variable)
  • Using variables before declaring them (ReferenceError)
  • Redeclaring let or const
  • Expecting const to make object immutable (it doesn’t)

Article 0 of 0