Skip to content

Commit ec00257

Browse files
committed
exec: explain-the-concept-of-scope-in-javascript
1 parent ed92473 commit ec00257

File tree

1 file changed

+27
-17
lines changed
  • questions/explain-the-concept-of-scope-in-javascript

1 file changed

+27
-17
lines changed

questions/explain-the-concept-of-scope-in-javascript/en-US.mdx

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,33 @@ title: Explain the concept of scope in JavaScript
66

77
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.
88

9-
```js
10-
// Global scope
11-
var globalVar = 'I am global';
9+
```js live
10+
var globalVar = 'I am a global var';
1211

1312
function myFunction() {
14-
// Function scope
15-
var functionVar = 'I am in a function';
13+
var functionVar = 'I am a function-scoped var';
1614

1715
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
2122
}
2223

23-
// console.log(blockVar); // Uncaught ReferenceError: blockVar is not defined
24+
console.log('Inside function:');
25+
console.log(globalVar); // Accessible
26+
console.log(functionVar); // Accessible
27+
// console.log(blockVar); // Uncaught ReferenceError
2428
}
2529

26-
console.log(globalVar); // Accessible here
27-
// console.log(functionVar); // Uncaught ReferenceError: functionVar is not defined
30+
myFunction();
31+
32+
console.log('In global scope:');
33+
console.log(globalVar); // Accessible
34+
// console.log(functionVar); // Uncaught ReferenceError
35+
// console.log(blockVar); // Uncaught ReferenceError
2836
```
2937

3038
---
@@ -35,47 +43,49 @@ console.log(globalVar); // Accessible here
3543

3644
Variables declared outside any function or block have global scope. They are accessible from anywhere in the code.
3745

38-
```js
46+
```js live
3947
var globalVar = 'I am global';
4048

4149
function myFunction() {
4250
console.log(globalVar); // Accessible here
4351
}
4452

53+
myFunction();
4554
console.log(globalVar); // Accessible here
4655
```
4756

4857
### Function scope
4958

5059
Variables declared within a function are in function scope. They are accessible only within that function.
5160

52-
```js
61+
```js live
5362
function myFunction() {
5463
var functionVar = 'I am in a function';
5564
console.log(functionVar); // Accessible here
5665
}
5766

58-
// console.log(functionVar); // Uncaught ReferenceError: functionVar is not defined
67+
myFunction();
68+
console.log(functionVar); // Uncaught ReferenceError: functionVar is not defined
5969
```
6070

6171
### Block scope
6272

6373
Variables declared with `let` or `const` within a block (e.g., within curly braces `{}`) have block scope. They are accessible only within that block.
6474

65-
```js
75+
```js live
6676
if (true) {
6777
let blockVar = 'I am in a block';
6878
console.log(blockVar); // Accessible here
6979
}
7080

71-
// console.log(blockVar); // Uncaught ReferenceError: blockVar is not defined
81+
console.log(blockVar); // Uncaught ReferenceError: blockVar is not defined
7282
```
7383

7484
### Lexical scope
7585

7686
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.
7787

78-
```js
88+
```js live
7989
function outerFunction() {
8090
var outerVar = 'I am outside';
8191

0 commit comments

Comments
 (0)