Scope Flashcards Preview

JavaScript Basics > Scope > Flashcards

Flashcards in Scope Deck (10)
Loading flashcards...
1
Q

Scope is ….

A

Scope is the idea in programming that some variables are accessible/inaccessible from other parts of the program.

2
Q

Global Scope refers to …

A

Global Scope refers to variables that are accessible to every part of the program.

3
Q

Block Scope refers to…

A

Block Scope refers to variables that are accessible only within the block they are defined.

4
Q

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

A

Uncaught ReferenceError: destination is not defined

5
Q

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

A variable that is accessible to any part of the program.

6
Q

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

A variable that is defined within a block and only available inside a block.

7
Q

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.

A

Ciao!

Goodbye

8
Q

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

A

number, phase, val

9
Q

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.

A

Defining variables in the block scope. Variables defined in the global scope can cause unexpected behavior in our code.

10
Q

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.

A

There are three: input, controlVal, and multiplier.