0% found this document useful (0 votes)
14 views

Learn JavaScript - Scope Cheatsheet - Codecademy

The document discusses different types of scopes in JavaScript including global, function, block, and file scopes. Variables declared with let or const are block scoped and only accessible within that block. Variables declared outside of blocks or functions exist in the global scope and are accessible throughout a program.

Uploaded by

ahmedahmedlaidi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Learn JavaScript - Scope Cheatsheet - Codecademy

The document discusses different types of scopes in JavaScript including global, function, block, and file scopes. Variables declared with let or const are block scoped and only accessible within that block. Variables declared outside of blocks or functions exist in the global scope and are accessible throughout a program.

Uploaded by

ahmedahmedlaidi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

15/11/2023 19:12 Learn JavaScript: Scope Cheatsheet | Codecademy

Cheatsheets / Learn JavaScript

Scope

Scope

Scope is a concept that refers to where values and function myFunction() {


functions can be accessed.
Various scopes include:
var pizzaName = "Volvo";
Global scope (a value/function in the global // Code here can use pizzaName
scope can be used anywhere in the entire
program)
}
File or module scope (the value/function can
only be accessed from within the file)
// Code here can't use pizzaName
Function scope (only visible within the function),

Code block scope (only visible within a { ... }


codeblock)

Block Scoped Variables

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);

// Uncaught ReferenceError: statusMessage


is not defined

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.
}

printColor(); // Prints: blue

Print Share

https://www.codecademy.com/learn/introduction-to-javascript/modules/learn-javascript-scope/cheatsheet 2/2

You might also like