Scope Flashcards

1
Q

What is the simplest definition of Scope?

A

Scope is the current context of execution. The context in which values & expressions are “visible” or can be referenced.

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

What is a more detailed meaning of Scope?

A

Scope is the accessibility of variables, functions, and objects in some particular part of your code during runtime.

In other words, scope determines the visibility of variables and other resources in areas of your code.

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

What is lexical scope?

A

When a function is defined in another function, the inner function has access to the outer function’s variables ie the inner function has access to the variables and resources of the parent scope.

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

When is a variable or other expression unavailable for use?

A

If a variable or another expression is not “in the current scope”, it is unavailable for use.

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

Do parent scopes have access to child scopes?

A

Scopes can be layered in hierarchy so that child scopes have access to parent scopes but not vice versa.

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

What sort of scope does a var statement declare?

A

The var statement declares a function-scoped or globally-scoped variable, optionally initializing it to a value.

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

How long does global scope live in your application?

A

Global scope lives as long as your application lives.

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

What sort of scope does a const statement declare?

A

Constants are block-scoped, much like variables declared using theletkeyword. The value of a constant can’t be changed through reassignment, and it can’t be redeclared.

This const declaration creates a constant whose scope can be either global or local to the block in which it is declared.

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

Does a global const declaration become part of the window object?

A

Global constants donotbecome properties of thewindowobject, unlikevarvariables.

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

Does a const declaration create an immutable value?

A

Theconstdeclarationcreates a read-only reference to a value. It doesnotmean the value it holds is immutable—just that the variable identifier cannot be reassigned. For instance, in the case where the content is an object, this means the object’s contents (e.g., its properties) can be altered.

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

Can a const share its name with a function or another variable?

A

A constant cannot share its name with a function or a variable in the same scope.

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

What sort of scope does a let statement declare?

A

Theletstatement declares a block-scoped local variable, optionally initializing it to a value.

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

How do the var and let declarations differ?

A

letallows you to declare variables that are limited to the scope of ablockstatement, or expression on which it is used, unlike thevarkeyword, which declares a variable globally, or locally to an entire function regardless of block scope. The other difference betweenvarandletis that the latter is initialized to a value only when aparser evaluates it.

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

Does a global let declarations become part of the window object?

A

Just likeconsttheletdoesnotcreate properties of thewindowobject when declared globally (in the top-most scope).

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

What is the scope chain?

A

The scope chain is used to resolve variables. Javascript starts at the innermost level of the code nest and keeps jumping bak to the parent scope until it finds the variable or any other resource it is looking for.

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

What is the temporal dead zone (TDZ)?

A

letvariables cannot be read/written until they have been fully initialized, which happens when they are declared(if no initial value is specified on declaration, the variable is initialized with a value ofundefined).Accessing the variable before the initialization results in aReferenceError.

This differs fromvarvariables, which will return a value ofundefinedif they are accessed before they are declared.

The variable is said to bein a “temporal dead zone” (TDZ)from the start of the block until the initialization has completed.

Using thetypeofoperator for aletvariable in its TDZ will throw aReferenceError:

17
Q

Which type os variable declarations does the Temporal Dead Zones relate to?

A

The TDZ relates to let and const declarations.