Learn JavaScript - Scope Cheatsheet - Codecademy
Learn JavaScript - Scope Cheatsheet - Codecademy
Scope
Scope
const and let are block scoped variables, meaning const isLoggedIn = true;
they are only accessible in their block or nested blocks.
In the given code block, trying to print the
statusMessage using the console.log() method will if (isLoggedIn == true) {
result in a ReferenceError . It is accessible only inside const statusMessage = 'User is logged
that if block. in.';
}
console.log(statusMessage);
https://www.codecademy.com/learn/introduction-to-javascript/modules/learn-javascript-scope/cheatsheet 1/2
15/11/2023 19:12 Learn JavaScript: Scope Cheatsheet | Codecademy
Global Variables
JavaScript variables that are declared outside of blocks // Variable declared globally
or functions can exist in the global scope, which means
const color = 'blue';
they are accessible throughout a program. Variables
declared outside of smaller block or function scopes
are accessible inside those smaller scopes. function printColor() {
Note: It is best practice to keep global variables to a console.log(color);
minimum.
}
Print Share
https://www.codecademy.com/learn/introduction-to-javascript/modules/learn-javascript-scope/cheatsheet 2/2