Variables Flashcards
Variables:
It is always best to declare a variable as locally as possible. When is it necessary to use global variables?
When data needs to be shared between functions in an application.
Variables:
Given
function flip(a, b) { temp = a; a = b; b = temp; }
Which variables are global?
Only temp is global
Because temp was not declared with the var statement inside the function JavaScript automatically assigns it to the global scope.
Variables:
True/False
Global variables are only global to the script in which they are declared.
False
Global variables can used by any script that is loaded by the running application.
Variables:
What is it called when the same global variable is used in more than one place for different purposes?
A collision.
Collisions are major source of difficult to diagnose bugs. It is best to avoid using global variables.
Variables:
What are rules called that govern where a variable is stored and how it will be retrieved?
Scope
Variables in JavaScript are scoped to the function that contains them. The outer most scope is the “global scope.”
Variables:
The JavaScript engine separates statements like “var a = 5” in two pieces at compile time. What happens before execution?
The variable “a” is declared in its scope.
The assignment is treated separately as a later step when the code executes.