Scope Flashcards
What is the simplest definition of Scope?
Scope is the current context of execution. The context in which values & expressions are “visible” or can be referenced.
What is a more detailed meaning of Scope?
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.
What is lexical scope?
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.
When is a variable or other expression unavailable for use?
If a variable or another expression is not “in the current scope”, it is unavailable for use.
Do parent scopes have access to child scopes?
Scopes can be layered in hierarchy so that child scopes have access to parent scopes but not vice versa.
What sort of scope does a var statement declare?
The var statement declares a function-scoped or globally-scoped variable, optionally initializing it to a value.
How long does global scope live in your application?
Global scope lives as long as your application lives.
What sort of scope does a const statement declare?
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.
Does a global const declaration become part of the window object?
Global constants donotbecome properties of thewindowobject, unlikevarvariables.
Does a const declaration create an immutable value?
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.
Can a const share its name with a function or another variable?
A constant cannot share its name with a function or a variable in the same scope.
What sort of scope does a let statement declare?
Theletstatement declares a block-scoped local variable, optionally initializing it to a value.
How do the var and let declarations differ?
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.
Does a global let declarations become part of the window object?
Just likeconsttheletdoesnotcreate properties of thewindowobject when declared globally (in the top-most scope).
What is the scope chain?
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.