You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: questions/explain-the-concept-of-scope-in-javascript/en-US.mdx
+27-17Lines changed: 27 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,25 +6,33 @@ title: Explain the concept of scope in JavaScript
6
6
7
7
In JavaScript, scope determines the accessibility of variables and functions at different parts of the code. There are three main types of scope: global scope, function scope, and block scope. Global scope means the variable is accessible everywhere in the code. Function scope means the variable is accessible only within the function it is declared. Block scope, introduced with ES6, means the variable is accessible only within the block (e.g., within curly braces `{}`) it is declared.
8
8
9
-
```js
10
-
// Global scope
11
-
var globalVar ='I am global';
9
+
```js live
10
+
var globalVar ='I am a global var';
12
11
13
12
functionmyFunction() {
14
-
// Function scope
15
-
var functionVar ='I am in a function';
13
+
var functionVar ='I am a function-scoped var';
16
14
17
15
if (true) {
18
-
// Block scope
19
-
let blockVar ='I am in a block';
20
-
console.log(blockVar); // Accessible here
16
+
let blockVar ='I am a block-scoped var';
17
+
18
+
console.log('Inside block:');
19
+
console.log(globalVar); // Accessible
20
+
console.log(functionVar); // Accessible
21
+
console.log(blockVar); // Accessible
21
22
}
22
23
23
-
// console.log(blockVar); // Uncaught ReferenceError: blockVar is not defined
@@ -35,47 +43,49 @@ console.log(globalVar); // Accessible here
35
43
36
44
Variables declared outside any function or block have global scope. They are accessible from anywhere in the code.
37
45
38
-
```js
46
+
```js live
39
47
var globalVar ='I am global';
40
48
41
49
functionmyFunction() {
42
50
console.log(globalVar); // Accessible here
43
51
}
44
52
53
+
myFunction();
45
54
console.log(globalVar); // Accessible here
46
55
```
47
56
48
57
### Function scope
49
58
50
59
Variables declared within a function are in function scope. They are accessible only within that function.
51
60
52
-
```js
61
+
```js live
53
62
functionmyFunction() {
54
63
var functionVar ='I am in a function';
55
64
console.log(functionVar); // Accessible here
56
65
}
57
66
58
-
// console.log(functionVar); // Uncaught ReferenceError: functionVar is not defined
67
+
myFunction();
68
+
console.log(functionVar); // Uncaught ReferenceError: functionVar is not defined
59
69
```
60
70
61
71
### Block scope
62
72
63
73
Variables declared with `let` or `const` within a block (e.g., within curly braces `{}`) have block scope. They are accessible only within that block.
64
74
65
-
```js
75
+
```js live
66
76
if (true) {
67
77
let blockVar ='I am in a block';
68
78
console.log(blockVar); // Accessible here
69
79
}
70
80
71
-
//console.log(blockVar); // Uncaught ReferenceError: blockVar is not defined
81
+
console.log(blockVar); // Uncaught ReferenceError: blockVar is not defined
72
82
```
73
83
74
84
### Lexical scope
75
85
76
86
JavaScript uses lexical scoping, meaning that the scope of a variable is determined by its location within the source code. Nested functions have access to variables declared in their outer scope.
0 commit comments