Scope Flashcards

1
Q

What is global scope?

A

-AKA global variables
-variables outside of a block of code
-can be accessed anytime, including in code blocks
Ex)
const color = ‘blue’;

const returnSkyColor = () => {
  return color; // blue 
};

console.log(returnSkyColor()); // blue

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

What is block scope?

A
When a variable is defined inside a block, it is only accessible to the code within the curly braces { }
Ex)
const logSkyColor = () => {
  let color = 'blue'; 
  console.log(color); // blue 
};

logSkyColor(); // blue
console.log(color); // ReferenceError

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

What is global scope pollution?

A

-too many global scope variables (considered bad practice)
-difficult to keep track of our different variables and sets us up for potential accidents.
Ex)
let num = 50;

const logNum = () => {
  num = 100; // Take note of this line of code
  console.log(num);
};

logNum(); // Prints 100
console.log(num); // Prints 100

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