-
Notifications
You must be signed in to change notification settings - Fork 9
/
01_06.js
76 lines (52 loc) · 1.15 KB
/
01_06.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
//==========PURE IMPURE FUNCTION==============
function add(x,y){
return x+y;
}
console.log(add(2,2))
function add(x,y){
let rand = Math.random()*10;
return x+y+rand;
}
//Math.random will generate random number between (0,1]
//Math.random() *10 will generate random no between 1 to 10
console.log(add(2,2))
let mutateNum = 0;
const impureFunction = (num) =>{
return (mutateNum+=num)
}
console.log(impureFunction(5))
console.log(impureFunction(5))
console.log(impureFunction(5))
console.log(impureFunction(5))
console.log(mutateNum)
//==========Closures================
//Nested function
function greet(nm){
function displayName(){
console.log('Hi'+" "+nm)
}
displayName();
}
greet('Dipanshu')
//returning a function
function greet(nm){
function displayName(){
console.log('Hi'+" "+nm)
}
return displayName;
}
let result = greet('Dipanshu');
console.log(result);
result();
//==========
function x(){
let a = 8;
function y(){
console.log(a)
}
a=120;
return y;
}
var z = x()
console.log(z) //function body
z()