-
Notifications
You must be signed in to change notification settings - Fork 0
/
scope.js
221 lines (91 loc) · 2.43 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
let a=5;
console.log(a);
let A="hi";
console.log(A);
//Global scope
var apple=5;
console.log(apple); //Using in global
function add(){
apple =apple+2; //Using inside a function
console.log(apple);
}
add();
//Block scope
console.log(c);
if(true){
var c=50; // First declaration inside the block
}
fun(); //Function call befor function defintion
function fun(){
console.log("hi");
}
//Anonymus functions and hoisting
function sun(){
console.log("First function");
}
var sun = function(){
console.log("Anonymous function");
}
function sun(){
console.log("Second function");
}
sun();
//Function scope
// Declaring variables inside function and Using them globally will produce reference error
function jail(){
var accust_name="sugan";
console.log(accust_name);
}
jail();
console.log(accust_name); //creates error message
//Simple scope accessibility rules:
function show(){
let rule=5; //Declaring inside a function
rule=4;
console.log(rule); //Using it in function
}
show();
rule=5;
console.log(rule); // Using it globally
// let ,var and const are same in global scope
var monday=1;
let tuesday=2;
const wednesday=3;
console.log(monday,tuesday,wednesday); ///using globally
function week(){
console.log(monday); //Using inside a function
console.log(tuesday);
console.log(wednesday);
}
week();
// let , const and var in block scope
if(true){
var jan=1; ///Declaring in block-scope
let feb=2;
const mar=3;
console.log(jan,feb,mar);
}
console.log(jan); //var will log the value
console.log(feb); //let will give reference error
console.log(mar); //const will also give reference error
//Hoisting var
console.log(jun);
var jun=55;
//Histing let
console.log(jul);
let jul=34;
console.log(aug);
const aug=100;
*/
// let, var and const are same in function scope
function games(){
var football=1; //declaring inside functons
let cricket=2;
const hockey=3;
console.log(football); //can work inside a function
console.log(cricket);
console.log(hockey);
}
games();
console.log(fotball); //cannot acces it globaly