forked from Swap76/Learn-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscope.js
More file actions
65 lines (56 loc) · 1.02 KB
/
Copy pathscope.js
File metadata and controls
65 lines (56 loc) · 1.02 KB
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
56
57
58
59
60
61
62
63
64
65
//JavaScript Scope
//Global scope
var a = 1;
function accessingGlobalVariable() {
console.log(a); // return 1
}
//End global scope
//Local scope
var a = 1;
function accessingGlobalVariable2() {
console.log(a); // return 1
}
function accessingLocalVariable() {
var a = 3;
console.log(a); // return 3
}
//End local scope
//Automatic global variable
function declaringAutomaticGlobalVariable() {
b = 'Hello';
}
declaringAutomaticGlobalVariable();
console.log(b); // return 'Hello'
//End automatic global variable
//Block scoping
//var
var a = 1;
function accessingVariable() {
if (true) {
var a = 4;
}
console.log(a);
}
accessingVariable(); // return 4
//end var
//let
var a = 1;
function accessingVariable() {
if (true) {
let a = 4;
}
console.log(a);
}
accessingVariable(); // return 1
//end let
//const
var a = 1;
function accessingVariable() {
if (true) {
const a = 4;
}
console.log(a);
}
accessingVariable(); // return 1
//end const
//End Block scoping