Skip to content

Commit 316c5e9

Browse files
committed
03_scope.js
Learned about Scope in Javascript that what is Global scope and what is block scope, how values of block scope treated outside the curly braces, difference between Global and block scope.
1 parent b29e2e5 commit 316c5e9

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

03_Basics/03_scope.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* 3. Scope
2+
3+
Note: {} This curly braces in every programming language is known as scope.
4+
eg. function {}, if {} else {}, for {} e.t.c, means it is scope of that function,
5+
loop, conditional e.t.c.
6+
Note: {} Curly braces in Object is Object Declaration. */
7+
8+
if (true) { // (true) states that we will go inside this if statement.
9+
let a = 10
10+
const b = 20
11+
var c = 30
12+
}
13+
// Note: The declared variables in "{}" should not exceed "}"
14+
// console.log(a); // Throw an error that 'a' is not defined
15+
// console.log(b); // Throw an error that 'b' is not defined
16+
// Note: It is good that variable a & b are not going outside the scope.
17+
// console.log(c); // Output: 30
18+
/* Note: This 30 should not come outside.That's the problem of var keyword.
19+
20+
--------------------------------------------------------------------------------------
21+
Global Scope, */
22+
let a = 100
23+
const b = 200
24+
var c = 300
25+
26+
// Block Scope,
27+
if (true) {
28+
let a = 10
29+
const b = 20
30+
var c = 30
31+
}
32+
33+
console.log(a); // Output: 100
34+
console.log(b); // Output: 200
35+
console.log(c); // output: 30
36+
37+
/* Note: Values of Global scope can be available inside {} but values written inside
38+
{} should not go outside {}
39+
40+
Note: The scope checked in browsers console and the scope checked in our code
41+
enviroment using node are different. */

0 commit comments

Comments
 (0)