-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04_hoisting.js
39 lines (29 loc) · 1.25 KB
/
04_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
/*-----------------------------------------------------------------------------*/
/* Hoisting is JavaScript's default behavior of moving all declarations to the top of the
// current scope (to the top of the current script or the current function).
-------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------*/
/* Undefined (JS knows that 'i' is declared later. Throws a ReferenceError)
-------------------------------------------------------------------------------*/
console.log(i);
var i = 42;
console.log(i);
// Const/let will throw an Error
console.log(x);
let x = 42;
console.log(x);
/*-----------------------------------------------------------------------------*/
/* Function Expression and Function Declaration
-------------------------------------------------------------------------------*/
// console.log(square(10));
// // Function Declaration (function can be accessed anywhere)
// function square(num){
// return num**2
// }
// console.log(square(5));
// console.log(square2(10));
// // Function Expression (function CANNOT be accessed before it is declared)
// var square2 = function(num) {
// return num ** 2
// }
// console.log(square2(5));