Scopes Flashcards

1
Q

What is a scope and how many types exist?

A

It’s a region of a program where a variable or function is defined and can be accessed.

There are two types of scopes:
- global scope
- local scope.

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

What is the difference between global and local scope?

A

local scope is created whenever a function is called, and any variable or function defined in this scope can only be accessed from within the function or nested functions.

The global scope is the outermost scope in a program, and any variable or function defined in this scope can be accessed from any other part of the program.

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

Why is it, in general, a good idea to leave the global scope of a website as-is and never touch it?

A

Every script has access to the global scope, and if everyone uses the global namespace to define their variables, collisions will likely occur. Use the module pattern (IIFEs) to encapsulate your variables within a local namespace.

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