forked from Safnaj/JavaScript-ES6
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathscope.js
More file actions
50 lines (35 loc) · 1.15 KB
/
scope.js
File metadata and controls
50 lines (35 loc) · 1.15 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
/************************************* EX 1 *************************************/
/*
- VAR keyword are scoped to the immediate function body.
- LET Variables are scoped to the immediate enclosing block {}.
*/
//let m = 0;
for (var i=0; i<10; i++) {
var k = i;
//let k = i;
//m=i;
console.log(">>> "+i);
}
console.log("last value :"+i);
console.log("inner value :"+k); //k is undefined if its let
console.log("last value :"+m); //m will print bcz we defined the variable & initialized
/************************************* EX 2 *************************************/
var tester = "hey hi";
function myFunction() {
var hello = "hello";
}
console.log(hello); // Error : hello is not defined | hello is outside of a function.
/************************************* EX 2 *************************************/
function run() {
var foo = "Foo";
let bar = "Bar";
console.log(foo, bar); // Foo Bar
{
var moo = "Mooo"
let baz = "Bazz";
console.log(moo, baz); // Mooo Bazz
}
console.log(moo); // Mooo - Inside the function.
console.log(baz); // ReferenceError - Outside the scope.
}
run();