JavaScript Day 8— Mastering JavaScript Basics: A Comprehensive Guide for Interview Preparation
100 Days of JavaScript Interview Prep: A Beginner’s Journey


Welcome to Day 8–100 Days of JavaScript Interview Prep: A Beginner’s Journey
Join us as we document a 100-day adventure filled with learning, practice, and growth.
DAY 8 (March 30, 2024) — Most Common Data Types Interview Questions
Online New JavaScript Playground with live changes
We will use online JavaScript Compiler today to run all examples of questions and Console window of browser today
Question 1 Difference between var, let, and const?
Answer :
var: Function-scoped, can be redeclared and reassigned.
// var example
var a = 'tofy';
var a = 'snow'; // Redeclaration allowed
console.log(a); // Output: snow
let: Block-scoped, can be reassigned but not redeclared within the same block scope.
// let example
let b = 10;
b = 20; // Reassignment allowed
console.log(b); // Output: 20
const: Block-scoped, cannot be reassigned or redeclared.
// const example
const c = 'test';
console.log(c); // output -test
Error Use Case when redeclared
// const example
const c = 'test';
c = 'world'; // Error: Assignment to constant variable
console.log(c); // Output: error 
Question 2 What is temporal dead zone (TDZ)?
Answer :
Temporal Dead Zone is a state where variables declared with let and const exist but cannot be accessed until after the declaration.
Trying to access variables in this state results in a Reference Error.
Run below directly in debugger console
console.log(myname); // ReferenceError: Cannot access 'myname' before initialization
let myname = 'kk';
VM667:1 Uncaught ReferenceError: myname is not defined

Const example
console.log(myname); // ReferenceError: Cannot access 'myname' before initialization
const myname = 'kk';
Question 3 Can you redeclare a variable using let and const?
Note : I am using direct console window of browser for running below examples
Answer :
No, you cannot redeclare a variable using let or const within the same scope.
It would result in a SyntaxError.
Please find examples with all data types
// Redeclaration with var
var myname = 'kk';
var myname = 'kk test'; // Allowed
// Redeclaration with let
let myNewName = 'testing kk';
let myNewName = 'tutor';
// Error: Identifier 'myNewName' has already been declared
// Redeclaration with const
const myHome = 'myRoom';
const myHome = 'myRoomwwithDog';
// Error: Identifier 'myHome' has already been declared
let and const | Identifier ‘myNewName’ has already been declared
let and constQuestion 4 How does var differ from let and const in terms of hoisting?
Answer :
- Variables declared with
varare hoisted to the top of their function or global scope and are initialized withundefined.
console.log(myname); // Output: undefined
var myname = 'kkkk';
This means you can access var variables before they are declared, but they will have the value undefined.
letandconstare also hoisted, but they are not initialized.
console.log(myname); // Output: undefined
let myname = 'kkkk';
VM1251:1 Uncaught ReferenceError: myname is not defined
at <anonymous>:1:13
Accessing them before their declaration results in a ReferenceError due to the temporal dead zone.
Question 5 When can we use let vs const?
Answer :
const — Use const when we don't intend to reassign the variable's value a
let — Use let when you need to reassign the variable’s value.
// When need to use Const values
const theme= "RED";
// Use let when you need to reassign the variable's value.
let count = 0;
count += 1;Stay Tuned and Happy learning :)Stay Tuned and Happy learning :)
Kind is cool !!! Share knowledge to gain knowledge !!!! Be kind and treat people with respect !!
If you find my videos helpful, then please subscribe, Claps and follow to support
Cheers, Kirti Kaushal

Others Articles






