avatarKirti K

Summary

The website content provides a comprehensive guide for JavaScript interview preparation, focusing on common data types questions, the differences between var, let, and const, temporal dead zone, redeclaration of variables, and hoisting, with examples and explanations for beginners.

Abstract

The web content is part of a "100 Days of JavaScript Interview Prep" series, specifically Day 8, which delves into the nuances of JavaScript data types and variable declarations. It explains the differences between var, let, and const in terms of scope, redeclaration, and reassignment. The article also covers the concept of the temporal dead zone, which affects let and const variables before their declaration. It clarifies when to use let versus const and discusses hoisting, illustrating how var is initialized to undefined upon hoisting, while let and const remain uninitialized and inaccessible until declared, leading to a temporal dead zone. The content aims to solidify understanding through direct examples and encourages readers to use an online JavaScript playground for hands-on practice. The author, Kirti Kaushal, provides additional resources and encourages kindness and knowledge sharing within the learning community.

Opinions

  • The author emphasizes the importance of understanding the subtle differences between var, let, and const for successful interview preparation.
  • There is a clear recommendation to use const for variables that should not be reassigned and let for those that should.
  • The content suggests that the temporal dead zone is a critical concept to grasp for modern JavaScript development.
  • The author believes that hands-on practice, such as using an online JavaScript playground, is essential for learning.
  • The article promotes a positive learning environment, advocating for kindness and respect among peers.
  • Kirti Kaushal encourages readers to subscribe and follow for more content, indicating a commitment to continuous learning and community support.

JavaScript Day 8— Mastering JavaScript Basics: A Comprehensive Guide for Interview Preparation

100 Days of JavaScript Interview Prep: A Beginner’s Journey

DAY 8 (March 30, 2024) — Most Common Data Types Interview Questions

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 
Assignment to constant variable

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
ReferenceError and temporal dead zone

Const example

console.log(myname); // ReferenceError: Cannot access 'myname' before initialization
const myname = 'kk';
ReferenceError: Cannot access ‘myname before initialization and temporal dead zone

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
redeclare a variable using let and const | Identifier ‘myNewName’ has already been declared
Error: Identifier ‘myHome’ has already been declared | redeclare a variable using let and const

Question 4 How does var differ from let and const in terms of hoisting?

Answer :

  • Variables declared with var are hoisted to the top of their function or global scope and are initialized with undefined.
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.

  • let and const are 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
ReferenceError due to the temporal dead zone.

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

JavaScript
Coding Interviews
Coding For Kids
Javascript Interview
Programming
Recommended from ReadMedium