Javascript Interview Questions ( Var, Let and Const ) - Hoisting, Scoping, Shadowing and more

RoadsideCoder
6 Mar 202214:43

Summary

TLDRThis video script discusses the intricacies of variable declaration in JavaScript, focusing on 'var', 'let', and 'const'. It explains scope, shadowing, and hoisting, highlighting how 'var' is function-scoped and can be redeclared, while 'let' and 'const' are block-scoped and cannot. The script delves into the temporal dead zone and the importance of understanding execution context. Aimed at interview preparation, it provides insights into common interview questions and the impact of variable declaration on JavaScript's behavior.

Takeaways

  • 📚 JavaScript has three ways to declare variables: `var`, `let`, and `const`, with `var` being the oldest and `let` and `const` introduced in ES6 to overcome some limitations of `var`.
  • 🔍 Scope is a critical concept in JavaScript, defining regions where variables can be recognized; it can be global, functional, or block scope.
  • 🚫 The `var` keyword declares variables with function scope or global scope, allowing for redeclaration without error, unlike `let` and `const` which are block-scoped and do not allow redeclaration in the same scope.
  • 🔑 Variable shadowing occurs when a variable declared with the same name in a inner scope takes precedence over an outer scope variable, which is allowed with `var` but not with `let` or `const` in the opposite scenario.
  • 🚫 `let` and `const` must be initialized at the time of declaration, unlike `var` which can be declared without initialization.
  • 🔄 Variables declared with `var` and `let` can be updated or reassigned, but `const` variables cannot be updated once initialized, attempting to do so results in an error.
  • 💡 Hoisting is a JavaScript mechanism where variable and function declarations are moved to the top of their containing scope during the creation phase, but `let` and `const` are in the 'Temporal Dead Zone' until they are initialized.
  • 🛑 The 'Temporal Dead Zone' (TDZ) is a period between declaration and initialization where `let` and `const` variables are in scope but cannot be accessed, leading to ReferenceError if attempted to be used before initialization.
  • 🤔 Interviewers often use questions about variable declaration, scope, hoisting, and TDZ to evaluate a candidate's understanding of JavaScript fundamentals.
  • 📈 The order of execution in JavaScript involves a creation phase where the context is set up, including hoisting, followed by an execution phase where the code is executed line by line.
  • 🔍 Understanding the nuances between `var`, `let`, and `const` is essential for effective JavaScript coding and is a common topic in technical interviews.

Q & A

  • What are the three ways to declare variables in JavaScript?

    -The three ways to declare variables in JavaScript are using 'var', 'let', and 'const'. 'var' has been in JavaScript since its inception, while 'let' and 'const' were introduced in ES6 to overcome some limitations of 'var'.

  • What is the concept of scope in JavaScript?

    -Scope in JavaScript refers to the region of a program where a variable exists and can be accessed. Beyond this region, the variable cannot be recognized. There are different types of scopes such as global scope, block scope, and function scope.

  • How does 'var' differ from 'let' and 'const' in terms of scoping?

    -'var' is function-scoped or globally-scoped if not declared within a function, whereas 'let' and 'const' are block-scoped, meaning they are only accessible within the block in which they are declared.

  • What is variable shadowing in JavaScript?

    -Variable shadowing occurs when a local variable with the same name as a variable in an outer scope is declared, effectively 'shadowing' the outer variable within the inner scope.

  • Why is it called 'illegal shadowing' when trying to shadow a 'let' variable with a 'var'?

    -It is called 'illegal shadowing' because you cannot redeclare a 'let' variable with a 'var' in the same scope, as 'let' is block-scoped and 'var' is function-scoped or globally-scoped, leading to a 'variable already declared' error.

  • Can 'var', 'let', and 'const' be redeclared in the same scope?

    -Yes, 'var' can be redeclared multiple times in the same scope without error. However, 'let' and 'const' cannot be redeclared in the same scope due to their block-scoping rules.

  • Why must a 'const' variable be initialized at the time of declaration?

    -A 'const' variable must be initialized at the time of declaration because it represents a constant value that cannot be reassigned. If not initialized, it would lead to an error stating 'missing initializer in const declaration'.

  • What is the difference between updating 'var' and 'let' variables compared to 'const' variables?

    -Both 'var' and 'let' variables can be updated after their initial assignment, meaning their values can be changed. In contrast, 'const' variables cannot be updated or reassigned once declared, as they are constants.

  • What is hoisting in JavaScript and how does it relate to 'var', 'let', and 'const'?

    -Hoisting is a JavaScript mechanism where variable and function declarations are moved to the top of their containing scope during the compilation phase. 'var' variables are hoisted to the top of their function or global scope, while 'let' and 'const' are placed in a 'temporal dead zone' until they are initialized.

  • What is the 'temporal dead zone' and how does it differ from hoisting?

    -The 'temporal dead zone' is the period between the declaration and initialization of 'let' and 'const' variables. During this time, the variables are in scope but have not yet been initialized, leading to a reference error if accessed before the initialization. This differs from hoisting, where 'var' variables are hoisted with an initial value of 'undefined'.

  • What is the significance of understanding hoisting and the 'temporal dead zone' in JavaScript interviews?

    -Understanding hoisting and the 'temporal dead zone' is significant because it demonstrates a deeper knowledge of JavaScript's execution context and variable scoping, which are common topics in technical interviews.

Outlines

00:00

📘 JavaScript Variable Declaration and Scope

This paragraph discusses the different ways to declare variables in JavaScript: 'var', 'let', and 'const'. It emphasizes the importance of understanding these concepts, especially in interviews, as they can set the tone for the rest of the interview. The paragraph explains the concept of scope, including global, functional, and block scopes, and how 'var' has function scope while 'let' and 'const' have block scope. It also introduces the concept of variable shadowing, where a variable declared with 'let' or 'const' can shadow a 'var' variable within the same scope, but not vice versa, leading to 'illegal shadowing' if attempted.

05:00

🛠 Variable Declaration Rules and Initialization

The second paragraph delves into the rules for declaring variables without initialization using 'var', 'let', and 'const'. It highlights that 'var' allows redeclaration and updating of variables, whereas 'let' and 'const' do not permit redeclaration within the same scope. 'Const' requires an initializer upon declaration and cannot be updated, unlike 'var' and 'let'. The paragraph also touches on the importance of understanding hoisting in JavaScript, explaining how variables and function declarations are moved to the top of their scope during the creation phase of the execution context.

10:02

🕵️‍♂️ Hoisting and the Temporal Dead Zone in JavaScript

This paragraph explores the concept of hoisting in JavaScript, explaining how variables and functions are treated during the execution context's creation phase. It contrasts 'var', which is hoisted and initialized with 'undefined', with 'let' and 'const', which are also hoisted but exist in a 'Temporal Dead Zone' until explicitly initialized. The explanation includes practical examples and potential interview questions that demonstrate the differences in behavior between 'var', 'let', and 'const'. The paragraph concludes with a real-world interview scenario that tests understanding of hoisting and the temporal dead zone.

Mindmap

Keywords

💡Variable Declaration

Variable declaration refers to the process of introducing a variable in a program by specifying its type and name. In the context of JavaScript, there are three ways to declare variables: using 'var', 'let', and 'const'. The video emphasizes the importance of understanding these declarations, especially for interviews, as they are fundamental to JavaScript and can affect the flow of an interview based on the candidate's response.

💡Scope

Scope defines the visibility and lifetime of a variable in programming. It is the region of the program where a variable is accessible. The video discusses different types of scopes such as global scope, functional scope, and block scope, and how they relate to variable declarations with 'var', 'let', and 'const'. For instance, 'var' has a functional scope or global scope, while 'let' and 'const' are block-scoped.

💡ES6

ES6, also known as ECMAScript 2015, is a version of the JavaScript language that introduced several new features, including 'let' and 'const' for variable declaration. The video mentions ES6 as the version that introduced these block-scoped variables to overcome limitations of 'var'.

💡Block Scope

Block scope is a type of scope where a variable is only accessible within the block (i.e., between the curly braces {}). 'let' and 'const' declarations are block-scoped, meaning they can only be accessed within the block they are declared in. The video illustrates this with examples showing that 'let' and 'const' variables throw a ReferenceError if accessed outside their block.

💡Global Scope

Global scope refers to the visibility of variables or functions that are accessible throughout the entire script, regardless of the block or function they are in. In the video, it is demonstrated that variables declared with 'var' have a global scope, meaning they can be accessed and modified from anywhere in the code.

💡Variable Shadowing

Variable shadowing occurs when a local variable has the same name as a variable in the outer scope, effectively 'shadowing' or hiding the outer variable within the inner scope. The video explains that with 'let' and 'const', shadowing is allowed within the same scope, but you cannot shadow a 'let' variable with a 'var', which is termed as 'illegal shadowing'.

💡Redeclaration

Redeclaration is the act of declaring the same variable more than once in the same scope. The video clarifies that 'var' allows redeclaration within the same scope without error, whereas 'let' and 'const' do not, as they will throw an error indicating that the variable has already been declared.

💡Initialization

Initialization is the process of assigning a value to a variable at the time of its declaration. The video explains that 'var' and 'let' allow variables to be declared without initialization, but 'const' requires an initializer, throwing an error if none is provided.

💡Temporal Dead Zone

The temporal dead zone (TDZ) is a period between the declaration and initialization of 'let' and 'const' variables where they are in an 'uninitialized' state and cannot be accessed. The video demonstrates that attempting to access a 'let' or 'const' variable before its formal initialization results in a ReferenceError.

💡Hoisting

Hoisting is a JavaScript mechanism where variable and function declarations are moved to the top of their containing scope during the compilation phase. The video explains that 'var' declarations are hoisted, meaning they can be referenced in code before their actual declaration. However, 'let' and 'const' are not hoisted in the same way; they exist in the TDZ until their declaration is executed.

💡Execution Context

An execution context in JavaScript is an environment created for the execution of code. It has two phases: creation and execution. The video describes how during the creation phase, variables and function declarations are initialized with 'undefined', and during the execution phase, the JavaScript engine assigns values and executes functions. Understanding execution contexts is crucial for grasping concepts like hoisting and TDZ.

Highlights

Three ways to declare variables in JavaScript: 'var', 'let', and 'const'.

'var' has been in JavaScript since its inception, while 'let' and 'const' were introduced in ES6.

Interviewers often use scope-related questions to gauge a candidate's JavaScript knowledge.

Scope is the region of a program where a variable exists and can be recognized.

Types of scopes include global scope, block scope, and function scope.

'var' is function-scoped, while 'let' and 'const' are block-scoped.

Variable shadowing allows a variable to be re-declared within a block scope, but not vice versa.

Re-declaring a variable with 'var' is allowed, but not with 'let' or 'const'.

'const' requires an initializer and cannot be updated, unlike 'var' and 'let'.

Understanding JavaScript execution context is crucial for grasping hoisting.

Hoisting is the behavior of JavaScript where variable and function declarations are moved to the top of their containing scope during the compilation phase.

Temporal Dead Zone (TDZ) is a period between the declaration and initialization of 'let' and 'const' variables.

Accessing a 'let' or 'const' variable before its initialization results in a ReferenceError.

Interviewers may ask about the console output of functions involving 'var', 'let', and 'const' to test understanding of hoisting and TDZ.

The importance of hoisting in JavaScript interviews and how it affects variable and function declarations.

Call stack is a mechanism that tracks function calls in JavaScript.

The video discusses how to handle questions on hoisting and TDZ in JavaScript interviews.

Encouragement for viewers to follow the presenter on Twitter and subscribe to the channel for more content.

Invitation for viewers to comment on topics they would like to see covered in future videos.

Transcripts

play00:00

where let and const three ways we can

play00:02

declare variables in javascript and

play00:04

there can be a ton of questions that can

play00:06

be made on this topic when it comes to

play00:08

interview so where existed in javascript

play00:11

pretty much ever since it was created

play00:13

and lit and const were introduced in es6

play00:15

version of javascript to overcome some

play00:17

of the limitations of wear so when i was

play00:19

preparing for this video i had a

play00:21

conversation with few interviewers and

play00:23

they all told me that these type of

play00:25

questions are generally asked at the

play00:27

beginning of the interview and they

play00:28

decide how the interview will go forward

play00:31

so let's say if the candidate failed to

play00:33

answer this question this can change the

play00:35

flow of interview drastically the

play00:37

interviewer can evaluate your javascript

play00:39

knowledge based on these questions so in

play00:41

this video i'll try to cover most types

play00:43

of questions that can be made on this

play00:45

topic so the first type of question that

play00:47

can be asked is on scope so i've opened

play00:50

html css and javascript files over here

play00:53

and when i run this

play00:59

yep

play01:00

so let's get back to our javascript file

play01:03

so first of all what is this thing

play01:04

called scope so a scope is a certain

play01:07

region of a program where a defined

play01:09

variable exists and can be recognized

play01:11

and beyond that it cannot be recognized

play01:14

so there can be multiple types of scopes

play01:16

for example global scope block scope

play01:18

functional scope so for example we are

play01:20

over here without any blocks or

play01:22

functions so this is our global scope if

play01:24

i create a function over here

play01:27

this is our function scope

play01:29

and if we create any other block over

play01:31

here this will be our block scope so

play01:34

when we talk about where so if i write

play01:36

where

play01:38

a

play01:40

equals five

play01:42

so this is in its global scope so it can

play01:45

be accessed anywhere over here so if i

play01:47

say console log a

play01:49

if we go to browser yup you see five

play01:52

output over here so where is functional

play01:55

scope but let and const are block scoped

play01:58

so let's see what that means so if we

play02:00

create a block over here

play02:03

and write where inside of it

play02:08

you're going to see that this is still

play02:10

accessible outside of this block

play02:13

but when we change it to let

play02:16

we're going to see reference error a is

play02:18

not defined so this is only accessible

play02:21

inside of this block so if i move this

play02:23

inside of it

play02:24

yup you're gonna see we are able to

play02:25

access it and same is with const

play02:30

it's accessible inside of it but when we

play02:32

take it outside

play02:33

nope a is not defined now there is

play02:35

another concept that can be taken out of

play02:37

this scoping which is called shadowing

play02:40

so in javascript the introduction of lit

play02:42

and const in es6 along with block

play02:44

scoping allows variable shadowing so

play02:46

what is variable shadowing if i write

play02:48

this code you're going to see we have a

play02:50

variable over here called hello and if

play02:53

we print this over here it's going to

play02:54

print hello yeah that's fine but if we

play02:57

have a block scope over here inside of

play02:59

this we say let a equals high let's say

play03:02

what do you think is going to happen

play03:04

this

play03:05

a is going to shadow this a this will

play03:08

overlap the value of this a but still

play03:10

it's just going to be only accessible

play03:12

inside of this block outside of this

play03:13

block the a will still be hello so if i

play03:16

run this

play03:19

you're gonna see first we get high from

play03:21

over here

play03:22

and then hello from over here so this is

play03:26

called variable shadowing now while

play03:28

shadowing a variable it should not cross

play03:30

the boundary of scope that is we can

play03:32

shadow where variable by using let but

play03:34

cannot do the opposite so if we try to

play03:36

shadow let variable by where variable it

play03:38

is known as illegal shadowing and it

play03:40

gives us the error that variable is

play03:42

already defined so let's check this out

play03:44

now you're gonna see we if we try to

play03:46

shadow where variable by using let it's

play03:49

gonna work absolutely fine but when we

play03:51

try to shadow let variable by using

play03:53

where it's going to give us the error so

play03:56

let's check this out if i run this

play04:00

you're gonna see

play04:02

b has been already declared so this is

play04:04

called illegal shadowing and sometimes

play04:06

all of these questions can be asked

play04:08

together by the interviewer as an

play04:10

extension to the scoping question now

play04:12

the next type of question can be on

play04:14

declaration how these three can be

play04:16

declared so if we try to say where a

play04:20

and then again say where a

play04:23

then this is absolutely fine it's not

play04:25

going to give us error we can redeclare

play04:26

it as many times as we want

play04:28

but when we say

play04:30

let a

play04:32

and

play04:32

let a again

play04:34

you're going to see that gives us the

play04:36

error a has already been declared so we

play04:38

cannot redeclare a variable by using let

play04:41

and when we use const

play04:48

it's again going to give us the same

play04:50

error but it's giving us another error

play04:52

missing initializer in const declaration

play04:54

which we are going to discuss in our

play04:55

next question so let in cons cannot be

play04:58

redeclared in the same scope but where

play05:00

can be redeclared in the same scope but

play05:02

if we have something like this

play05:05

and we say let over here

play05:07

and we save this then this is completely

play05:09

fine which we already discussed that

play05:11

this comes under shadowing now the next

play05:13

type of question can be declaration

play05:15

without initialization so if i write

play05:18

where a and we declare it just like this

play05:20

then this is absolutely fine no problem

play05:22

we can declare it without providing it

play05:24

any value

play05:25

okay so but if we try to do it with lit

play05:28

then also this is completely fine but if

play05:30

we do it with const

play05:32

then this will give us error that

play05:33

missing initializer in const declaration

play05:36

we need to provide cons some value while

play05:38

declaring it so it cannot be declared

play05:40

without initializing it with any value

play05:42

all right pause if you're not yet

play05:44

following me on twitter go to

play05:46

twitter.com push underscore eon or click

play05:48

the link in the description down below

play05:50

and hit that follow button right now

play05:52

i'm waiting for you

play05:55

i'm still waiting

play05:58

okay fine let's continue with the video

play06:00

now just like declaration there can be

play06:02

questions on initializations as well so

play06:04

can we re-initialize these variables

play06:06

let's see so if we say where a equals

play06:08

five

play06:10

and we again try to say a equals six

play06:13

yep that's fine we can do that

play06:15

but what about lit

play06:18

yup we can do that with let as well

play06:20

but can we do that with const

play06:23

no we cannot do that with cons

play06:24

assignment to a constant variable we get

play06:27

this error over here so where and lit

play06:29

can be updated but const can never be

play06:31

updated if done this will give us this

play06:34

assignment to constant variable error

play06:36

now the next type of question that can

play06:37

be asked is on hosting and this is a

play06:40

really really important topic 95 of

play06:42

javascript interviews definitely have a

play06:45

question on hosting but before

play06:47

understanding hosting let's understand

play06:49

how javascript execution context works

play06:53

so what happens is when we try to

play06:55

execute a js code there are two phases

play06:58

one is a creation phase and the other is

play07:00

execution phase so in creation phase

play07:03

three things happen first it creates a

play07:05

global or a window object so

play07:08

i'm going to create a

play07:11

window object representation over here

play07:13

oh man i'm so bad at drawing the second

play07:15

step is it setups a memory heap for

play07:17

storing variables and function

play07:18

references that means it takes all of

play07:21

the variables and functions and stores

play07:23

it inside of this window object and the

play07:26

third step is that it initializes those

play07:28

functions and variable declarations with

play07:31

undefined so let's say we have first

play07:33

variable as a so we're going to store a

play07:36

second is our function which is multiply

play07:39

so we stored multiply over here and the

play07:42

third thing is our variable b

play07:44

now the third step is that it

play07:46

initializes them with undefined and for

play07:48

the function declarations it takes the

play07:50

whole complete function from here and

play07:52

stores it inside of our window object

play07:54

and this is the exact reason why hosting

play07:56

occurs i'm going to talk about hosting

play07:58

in just a minute but let's understand

play08:00

what happens in the execution phase so

play08:03

during the execution phase the

play08:04

javascript engine executes the code line

play08:06

by line assigning the values to

play08:09

variables and executes the function

play08:10

calls also for every new function

play08:13

created javascript engine creates a new

play08:15

execution context altogether we're gonna

play08:17

talk about how function work with

play08:19

execution context in another video of

play08:21

this series for this video i'm gonna

play08:23

focus on where let enconst so during the

play08:25

execution phase javascript first assigns

play08:27

the value of a which is 10 then it moves

play08:31

on to variable b because the function

play08:32

multiplier has not been executed yet so

play08:35

it's not going to touch this function

play08:36

then it takes b and assigns it with this

play08:40

function execution so it's going to

play08:41

execute the function with multiply x so

play08:43

it's going to pass 10 to this and it's

play08:45

going to multiply to provide it value

play08:47

100 and then it's going to run this

play08:49

console log b which is going to be the

play08:50

output of 100. now javascript also uses

play08:53

something called call stack which is a

play08:55

mechanism to keep track of all of the

play08:56

function calls which we will discuss in

play08:59

our upcoming videos so now you know how

play09:01

javascript execution context works let's

play09:03

move forward to hosting so during the

play09:06

creation phase javascript engine moves

play09:08

your variables and function declarations

play09:10

to the top of your code and this is

play09:12

known as hosting if i declare a variable

play09:14

called count and if i try to console log

play09:18

it before it was declared

play09:20

you're gonna see that we get undefined

play09:22

we didn't get any error it should have

play09:24

given us error right that the variable

play09:26

is not declared yet but since we know

play09:28

how the javascript execution context

play09:30

works it declares all of these variables

play09:33

and functions at the top of the code

play09:35

during the creation phase and then when

play09:37

the execution happens it checks if this

play09:39

variable already exists during the

play09:41

creation phase or not so obviously it

play09:43

existed so it gives us undefined so how

play09:46

javascript looks at this code is it

play09:48

looks at like this

play09:51

that yep where was already declared and

play09:53

then we are console logging this and

play09:55

then later we are initializing it okay

play09:58

so this was the case of where right but

play10:00

what happens in let

play10:02

let's change this to let

play10:04

are let variables hosted as well

play10:08

well if you console log this you're

play10:10

gonna see this error and you're gonna

play10:11

say no they are not hosted but you're

play10:13

wrong they are hosted they are hosted in

play10:15

temporal dead zone we're going to talk

play10:17

about that temporal dead zone in just a

play10:18

minute but let's see what do we get over

play10:20

here so we get cannot access count

play10:22

before initialization so this basically

play10:25

helps us overcome the limitations of

play10:26

where so where didn't warn us about the

play10:29

declaration but since we are using let

play10:32

it says that we cannot access the count

play10:34

before the initialization

play10:36

now if we go

play10:40

over here in the

play10:42

sources

play10:43

and put a breakpoint over here and when

play10:45

then we try to run this

play10:47

you're going to see

play10:49

that inside of the script we have this

play10:52

count as undefined whereas if we let's

play10:54

try to declare another variable with var

play10:58

count

play11:00

two

play11:01

two value

play11:02

and if you go back and refresh this

play11:06

you're gonna see in the global we have

play11:08

this count two over here so this is

play11:10

inside of our scope and this is

play11:12

undefined so that's fine but you're

play11:13

going to see we have a separate script

play11:15

over here which has this count variable

play11:18

which is inside of the temporal dead

play11:19

zone so temporal dead zone is the time

play11:21

between the declaration and the

play11:23

initialization of let and const

play11:25

variables so let's understand hosting by

play11:28

understanding this question which was

play11:29

asked to me in one of my previous

play11:31

interviews so i've already made a video

play11:33

on this so i'm going to play that clip

play11:35

right over here so interviewer gave me

play11:36

this function right here function abc

play11:39

and he asked what is going to be the

play11:41

console log of a so i want you to think

play11:44

about this first before moving forward

play11:46

because we are using where over here to

play11:48

declare this variable so what's going to

play11:50

happen is when this function is

play11:52

initialized in our execution context

play11:54

it's going to host this variable a and

play11:56

the console log will be undefined so let

play11:59

me show you real quick

play12:01

if i say abc

play12:03

and run this

play12:04

you're gonna see that we get undefined

play12:07

in the console so

play12:09

yep you see we get this undefined now

play12:11

why is that so if you go to sources

play12:14

and right over here in the script.js i'm

play12:17

going to put a breakpoint over here in

play12:19

the console log a so now if i refresh

play12:22

the page

play12:23

you're going to see when the code

play12:25

reaches over here a is undefined because

play12:28

it has initialized this function but it

play12:30

has not initialized this variable yet so

play12:33

this variable a is undefined at this

play12:35

moment but if the console log was after

play12:37

this variable a then obviously it would

play12:39

have printed the value of a which which

play12:41

is going to be 10.

play12:43

so now if i

play12:45

put another breakpoint over here and say

play12:47

move forward

play12:50

now you're gonna see a is 10 right over

play12:53

here when this function ends the value

play12:55

of a will be 10 but since at this time

play12:58

the value of a was undefined it's going

play13:00

to print undefined

play13:02

now then interviewer added few more

play13:04

variables inside of this function

play13:06

now then he asked me what is going to be

play13:08

the console log for all of these three

play13:10

values so obviously we know for a it's

play13:12

going to be undefined but for b and c

play13:14

what is it going to be because const and

play13:16

led behave a little differently than

play13:17

where are these two variables going to

play13:19

be hosted as well

play13:21

yes they are going to be hosted but they

play13:23

are going to be hosted in temporal dead

play13:24

zone so if i go back

play13:27

and you're going to see we get this

play13:28

error cannot access b before the

play13:30

initialization so our constant let

play13:32

variables are not hosted like where but

play13:34

they are hosted in the temporal dead

play13:36

zone so if i go

play13:37

to sources so now if i put a debugger

play13:40

over here and let's see here as well and

play13:43

refresh this

play13:45

you're gonna see that we still get b and

play13:47

c undefined in our local scope but these

play13:50

doesn't work like exactly like where

play13:53

does these both will be initialized in

play13:55

the temporal dead zone so what is

play13:56

temporal dead zone you ask so temporal

play13:58

dead zone is the term to describe the

play14:00

state where variables are in the scope

play14:02

but they are not yet declared

play14:04

just like over here they are in the

play14:06

scope but they have not been declared

play14:09

yet so that's why b and c are going to

play14:12

be in the temporal dead

play14:14

zone so these were some of the major

play14:17

types of questions that can be asked to

play14:19

us during these javascript interviews

play14:21

now obviously there can be more types of

play14:23

questions that can be framed by using

play14:24

lit wear and const do mention them in

play14:26

the comments down below so that others

play14:28

can know and i can make a video on them

play14:30

in future so if you like this first

play14:32

video of our javascript interview series

play14:34

give this video a huge fat thumbs up and

play14:36

subscribe to the channel for more such

play14:37

awesome videos and let me know in the

play14:38

comments down below which topic would

play14:40

you like for me to cover next

Rate This

5.0 / 5 (0 votes)

الوسوم ذات الصلة
JavaScriptVariable ScopeInterview PrepES6 FeaturesCoding TutorialGlobal ScopeBlock ScopeVariable ShadowingHosting ConceptTemporal Dead Zone
هل تحتاج إلى تلخيص باللغة الإنجليزية؟