Scope Flashcards
Scope is ….
Scope is the idea in programming that some variables are accessible/inaccessible from other parts of the program.
Global Scope refers to …
Global Scope refers to variables that are accessible to every part of the program.
Block Scope refers to…
Block Scope refers to variables that are accessible only within the block they are defined.
What will be the output of this code?
const roadTrip = () => { const destination = 'Crater Lake, Oregon'; const snacks = ['jerky', 'chocolate covered almonds', 'water']; const supplies = ['tent', 'camp stove', 'sleeping bag']; }
console.log(‘Next stop: ‘, destination);
What will be the output of this code?
Next stop: destination
Next stop: Crater Lake, Oregon
Uncaught ReferenceError: destination is not defined
Uncaught ReferenceError: destination is not defined
What is a globally scoped variable?
What is a globally scoped variable?
A variable that is defined in a function.
A variable that is also a parameter.
A variable that is accessible to any part of the program.
A variable that is accessible in a block, and only inside a block.
A variable that is accessible to any part of the program.
Which best defines a variable with block scope?
Which best defines a variable with block scope?
A variable that is available outside of a block.
A variable that is available within a function.
A variable that is available throughout a program.
A variable that is defined within a block and only available inside a block.
A variable that is defined within a block and only available inside a block.
What will be the output of this code?
let sayHello = 'Hi there'; const sayGoodbye = 'Goodbye';
const speakItalian = () => { sayHello = 'Ciao!'; console.log(sayHello); console.log(sayGoodbye); }
speakItalian();
What will be the output of this code?
Ciao!
Goodbye
Hi there
Goodbye
Ciao!
ReferenceError: variable not defined.
Ciao!
Goodbye
Which variables possess block scope?
const input = prompt('Enter input value'); const controlVal = input / 2 + 3; const multiplier = (number, phase) => { const val = number * controlVal + phase; console.log(val); }; Which variables possess block scope?
number, phase, val
input, controlVal, multiplier
input, controlVal
val, number, controlVal, phase
number, phase, val
What is preferable: defining variables in the global scope or defining variables in the block scope?
What is preferable: defining variables in the global scope or defining variables in the block scope?
Defining variables in the global scope. Variables defined in the block scope are restrictive and often conflict with variables defined in the global scope.
Defining variables in the block scope. Variables defined at the block level can still be used at the global level.
Defining variables in the block scope. Variables defined in the global scope can cause unexpected behavior in our code.
Defining variables in the global scope and the block scope are equally preferable.
Defining variables in the block scope. Variables defined in the global scope can cause unexpected behavior in our code.
How many global variables are there in the following block of code?
const input = prompt('Enter input value'); const controlVal = input / 2 + 3; const multiplier = (number, phase) => { const val = number * controlVal + phase; console.log(val); }; How many global variables are there in the following block of code?
There are four: input, controlVal, val, and multiplier.
There are three: input, controlVal, and multiplier.
There are two: input and `controlVal.
There are three: input, controlVal, and val.
There are three: input, controlVal, and multiplier.