JavaScript: Scope Flashcards
A variable declared in global scope is available to ____________?
All functions
A variable declared in local scope is only available to ____________?
That function
Global Scope in Syntax
const hello = “Hello”;
function sayHello() {
console.log(hello);
return;
}
const sayHelloAgain = function () {
console.log(hello);
return;
};
sayHello();
sayHelloAgain();
Local Scope in Syntax
function sayGoodbye() {
const goodbye = “Goodbye”;
console.log(goodbye);
return;
}
What is shadowing?
Shadowing happens when the same variable is used in the local and global scope.
Shadowing in Syntax
const shadow = “Hello”;
console.log(shadow);
function sayWhat() {
console.log(shadow);
return;
}
const sayWhatAgain = function () {
const shadow = “Goodbye”;
console.log(shadow);
};
sayGoodbye();
sayWhat();
sayWhatAgain();