-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhoisting.js
54 lines (49 loc) · 1.22 KB
/
hoisting.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
/**********************************
* Hoisting with funtions
*/
function calculateAge(year) {
console.log('line 5', 2019 - year);
}
calculateAge(1993);
//-----------------------------
calculateAge1(1993);
function calculateAge1(year) {
console.log('line 11', 2019 - year);
}
//------------------------------
var retirement = function(year) {
console.log('variable expression', 65 - (2019 - year));
}
retirement(1993);
/***********************************************
* the below code will thrown an error,
* Uncaught Error, retirement1 is not a function
*/
// retirement1(1999);
// var retirement1 = function(year) {
// console.log(65 - (2019 - year));
// }
/********************************************
* Hoisting with variables
*/
console.log('line 31', age);
var age = 23;
console.log('line 33', age);
//-------------below code thrown an error of bar is not a defined
// console.log(bar);
function foo() {
var age = 65;
console.log('line 40', age);
}
foo();
console.log('line 43', age);
var a = 2;
foo1(); // works because `foo()`
// declaration is "hoisted"
function foo1() {
a = 3;
console.log( a ); // 3
var a; // declaration is "hoisted"
// to the top of `foo()`
}
console.log( a ); // 2