-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhoisting.js
37 lines (28 loc) · 993 Bytes
/
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
/*
# Hoisting - All variable declarations inside/outside the function is hoisted at the top. This is true for function declaration as well.
## Reference
1. https://code.tutsplus.com/tutorials/javascript-hoisting-explained--net-15092
2. https://www.sitepoint.com/back-to-basics-javascript-hoisting/
*/
// Example and see below for explanation
console.log(x); // undefined
var x = 10; // Global variable
funz(); // Below function is available here too, since it is also hoisted at the top.
function funz() {
console.log(y); // undefined
var y = 20; // Local variable
console.log(y); // 20
}
console.log(x); // 10
// Above example goes like below javascript
var x; // Declaration
console.log(x); // undefined
x = 10; // Initialization
funz(); // Hoisting a function is true only for function declaration, not for function expression.
function funz() {
var y; // Declaration
console.log(y); // undefined
y = 20; // Initialization
console.log(y); // 20
}
console.log(x); // 10