-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhoisting.js
128 lines (77 loc) · 2.68 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// variable hoisting example
//console.log(a) // output: error a is undefined
console.log(x) // output: 10
var x = 10
// uporer example a output 10 aise karon hoisting. jokhon js compiler code execute kore tokhon
//sokol var diye declare variable ke upore niye tarpor execution kore
//example: uporer code ta ke amra aivabe kolpona korte pari
// var x
// console.log(x)
// x = 10
// this is hoisting
// console.log(e) // output: can not access e before initialization.
// let e = 10
//because jokhon hoisting hoy tokhon var ar khetre automaticlly
// undefiend assignment hoye jay.but let ar khetre ata hoy na..
//example: hoisting:
// let x
//console.log(x)
//x= undefined
//x = 10
let y;
console.log(y) // output: undefined
//because when declate let variable automatically assign undefined like y = undefined
// another hoisting example
var LANGUAGE = 'JAVA'
var language = 'javascript'
function getLanguage() {
if(!language){
var language = LANGUAGE;
}
return language;
}
console.log(`I love ${getLanguage()}`) // output: I love JAVA
//because hoisting hoy hosse every scope.var hosse function scope tai prothome getLanguage function a var language; hoisting hoy; and language undefined tahke tai
//condition true hoy and language change hoye JAVA hoy jay;
let L = 'JAVA'
let l = 'javascript'
function getLanguageTwo() {
if(!l){
let l = L;
}
return l;
}
console.log(`I love ${getLanguageTwo()}`) // output: I love javascript
// jeheto hoisting hoy every block or scope a aikhane let hosse block scope variable tai if block a
//vitore let l hoisting hoy and
//if block a condition a
// l prothome tar perent scope a khuje l k tarpor global scope a khuje
// jeheto global scope l declare and assign kora hoy seheto if condition false
// tai javascript return kora .
// function hoisting example
// function hoisting ar khetre function defination sobar prothome
// execution hoy and pore function call. so ai karone following output ata astese
myFunc() // output: I love javascript
function myFunc() {
console.log('I love javascript!')
}
myFuncTwo() // output: javascript
function myFuncTwo() {
language = "javascript"
var language;
console.log(language)
}
// but when function declare in variable ..function hoisting work like variable hoisting
//example
// myFuncThree() // output: myFuncThree is not a function
// var myFuncThree = function () {
// language = "javascript"
// var language;
// console.log(language)
// }
// myFuncFour() // output: Cannot access 'myFuncFour' before initialization
// const myFuncFour = function () {
// language = "javascript"
// var language;
// console.log(language)
// }