Variables Flashcards

0
Q

Variables:

It is always best to declare a variable as locally as possible. When is it necessary to use global variables?

A

When data needs to be shared between functions in an application.

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

Variables:

Given

function flip(a, b) {
   temp = a;
   a = b;
   b = temp;
}

Which variables are global?

A

Only temp is global

Because temp was not declared with the var statement inside the function JavaScript automatically assigns it to the global scope.

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

Variables:
True/False

Global variables are only global to the script in which they are declared.

A

False

Global variables can used by any script that is loaded by the running application.

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

Variables:

What is it called when the same global variable is used in more than one place for different purposes?

A

A collision.

Collisions are major source of difficult to diagnose bugs. It is best to avoid using global variables.

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

Variables:

What are rules called that govern where a variable is stored and how it will be retrieved?

A

Scope

Variables in JavaScript are scoped to the function that contains them. The outer most scope is the “global scope.”

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

Variables:

The JavaScript engine separates statements like “var a = 5” in two pieces at compile time. What happens before execution?

A

The variable “a” is declared in its scope.

The assignment is treated separately as a later step when the code executes.

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