forked from HackYourFuture/JavaScript2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscope.js
More file actions
58 lines (45 loc) · 1.42 KB
/
scope.js
File metadata and controls
58 lines (45 loc) · 1.42 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
// Global scope variable available throughout the application.
// Try doing console.log(globalVar) in callbacks.js or closures.js
const globalVar = 100;
{
'use strict';
// Exercise #1
// const firstLocalFunction = () => {
// const localVar = "I am a local variable";
// console.log('INSIDE FIRST LOCAL FUNCTION', localVar, globalVar);
// };
// firstLocalFunction();
// Exercise #2
// const secondLocalFunction = () => {
// // can use the same variable name here because of local scoping
// const localVar = 10;
// const nestedFunction = () => {
// const nestedVar = "I am a nested variable";
// console.log('INSIDE NESTED FUNCTION', nestedVar, localVar, globalVar);
// };
// // nestedVar is not visible here
// nestedFunction();
// };
// secondLocalFunction();
// localVar defined inside the functions isn't visible here
// Exercise #3
// const myFunction = () => {
// const localVar = 10;
// if (localVar === 10) {
// const innerVar = 100;
// console.log('INSIDE IF BLOCK', localVar, innerVar);
// }
// console.log(innerVar);
// };
// myFunction();
// Exercise #4
// const trickFunction = () => {
// const arr = [10, 12, 15, 21];
// for (var i = 0; i < arr.length; i++) {
// setTimeout(function() {
// console.log('Index: ' + i + ', element: ' + arr[i]);
// }, 3000);
// }
// };
// trickFunction();
};