Scope Flashcards
Whats the difference between Global Scope and Block Scope?
Global scope refers to the variables that can be accessed to every part of the program. Where as, Block Scope refers to the variables that can only be accessed within the block they’re defined in.
You should modify your function if it has one parameter, sole-single line block and one more thing. Name these three things and then modify the following function:
const getFahrenheit = (celsius) => { return multiplyByNineFifths(celsius) + 32; };
console.log(‘The temperature is ‘ + getFahrenheit(15) + ‘°F’);
- No curly braces
- Value in parameter does not need parenthesis around it
- The return keyword can be removed too
const getFahrenheit = celsius => multiplyByNineFifths(celsius) + 32;
console.log(‘The temperature is ‘ + getFahrenheit(15) + ‘°F’);
What is preferable: defining variables in the global scope or defining variables in the block scope? Choose answer and then give reason to why.
Defining variables in the block scope, defining variables 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); };
- input
- controlVal
- multiplier