New Variables — Creation, Updating and Scoping Flashcards

1
Q

What is the most important new feature of Let and Const variables?

A

They are Block Scoped.

var is function scoped.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Can var variables be updated?

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Can var variables be redefined?

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Name the most important benefit of using let and consts and explain why it is important.

A

Rather than being scoped to the function, it is scoped to the block.

This prevents variables leaking out of blocks into parent functions and even the window in certain cases.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you spot a block statement?

Name an example.

A

An opening and closing bracket is a block statement.

e.g an if statement

if {
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Can you update a variable

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Can you redefine a redefine a variable in the same scope

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Can you redefine a let in the same scope

A

No, it will throw an error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why is it useful that let and const don’t allow you to redefine them?

A

Prevents duplicates through accidents that can cause bugs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Can const variables be updated?

A

No. But with objects properties my be updated.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Can let variables be updated?

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

In a script tag you write:

let result = true inside an if statement;

what would you get if you logged result in the browser and why?

A

Nothing because the let variable is block scoped locally inside an if statement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

In a script tag you write:

let result = true

what would you get if you logged result in the browser and why?

A

You would get ‘true’.

Because the let var is block scoped it would be attached to the window and can therefore be found by a console.log

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Can you reassign a const variable object.

Name an example that illustrates this relationship.

A

You can’t reassign them. The same object will always be used and cannot be wiped.

Properties updatable.

Just like a person you wouldn’t change their name or body, but their age be changed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Two reasons for using let and const

A

Scope something to a block

Creating a variable that cannot be changed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does an IIFE stand for?

A

Immediately invoked function expression

17
Q

What is an IIFE used for and why is was it useful?

A

An immediately invoked function expression is a self running function often used to prevents var variables leaking into the global scope.

18
Q

What is the ES6 answer to the IIFE?

A

let and const are block scoped and therefore just require two brackets:

{
   let name = "harry"
}
19
Q

What is a problem loops with var variables suffer from?

A

The var variable in a loop is function scoped and therefore leaks into the window or parent scope.

Just a placeholder value so this behaviour is not necessarily desired.

20
Q

Name one benefit of using let variables for loops?

A

The loop’s let variable is block scoped to the loop itself helping to reduce bugs.

21
Q

What is the temporal dead zone?

A

Cannot access variable before it is created.

When variables are accessed before they are defined.
These return undefined.

e.g. console.log(car)
< TEMPORAL DEAD ZONE >
var car = “audi”;

22
Q

How do let and const behave with the temporal dead zone?

A

They provide a proper error which will break code as opposed to var’s which just return undefined.

23
Q

Is var dead? What Should I use?

A

Var should never be used.

Use Const by default.

Use Let if you need to update it.