-
Notifications
You must be signed in to change notification settings - Fork 0
/
scope.js
55 lines (32 loc) · 1.76 KB
/
scope.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
Scope of Variable
Scope is a concept that refers to where values and functions can be accessed.
Various scopes include:
-Global scope (a value/function in the global scope can be used anywhere in the entire program)
-File or module scope (the value/function can only be accessed from within the file)
-Function scope (only visible within the function),
-Code block scope (only visible within a { ... } codeblock)
- // example
function myFunction() {
var pizzaName = "Volvo";
// Code here can use pizzaName
}
// Code here can't use pizzaName
-- Global Variables
JavaScript variables that are declared outside of blocks or functions can exist in the global scope, which means they are accessible throughout a program. Variables declared outside of smaller block or function scopes are accessible inside those smaller scopes.
Note: It is best practice to keep global variables to a minimum.
// Variable declared globally
const color = 'blue';
function printColor() {
console.log(color);
}
printColor(); // Prints: blue
-- Block Scoped Variables
const and let are block scoped variables, meaning 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 result in a ReferenceError. It is accessible only inside that if block.
const isLoggedIn = true;
if (isLoggedIn == true) {
const statusMessage = 'User is logged in.';
}
console.log(statusMessage);
// Uncaught ReferenceError: statusMessage is not defined
--Note: We should not use global scope unless required as it is a bad practice.
Scope should be limited till the variable is used. Also it makes the code looks clean and increases readability for other Developers.