-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.js
137 lines (110 loc) · 3.28 KB
/
function.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
129
130
131
132
133
134
135
136
137
// function is a block of reusable code that performs a specific task or calculates a value
// function doesn't have a name it's called anonymous function
// Function Declarations
function fnName() { console.log("fnName") }
fnName();
const fnName1 = function () { console.log("fnName1") }
fnName1();
// with parameters
function fnName2(params) { console.log(params) }
fnName2("kalidas"); // "kalidas" is argument
// return function
function sum(x, y) { return x + y }
console.log(sum(1, 2)); // 3
// default Parameter
function calc(x, y = 10) { return x + y }
console.log(calc(5, 5)) // 10
console.log(calc(5)) // 15
// rest Parameter
// Is something that allows a fn to accept any number of arguments
// only one rest parameter in fn and rest parameter must be last
function display(params, ...restParams) {
console.log(params)
console.log(restParams)
}
display("hi", "kalidas", "software developer");
// arrow function or fat arrow syntax
// arrow fun not binding this keyword
const sub = (x, y) => x - y;
console.log(sub(10, 5));
// closure function
/* a closure is a combination of a function and the lexical
environment within which that function was declared. This means
that a closure has access to variables and parameters of its outer function even
after the outer function has finished executing */
// or nested fn is a closure
function outer(x) {
function inner(y) {
return x + y
}
return inner;
}
const outerFn = outer(10);
console.log(outerFn(5)) // 10
// callback function
/* A callback function in JavaScript is a function that is passed as an argument to
another function and is executed after some asynchronous operation,
Callback functions are commonly used in event handling, AJAX requests,
and asynchronous programming */
function doSomethingAsync(callback) {
setTimeout(function () {
console.log("Async operation completed");
callback();
}, 2000);
}
function callbackFunction() {
console.log("Callback function executed");
}
doSomethingAsync(callbackFunction);
// high order function (HOF)
// HOF is a function, it's take one or more fn as argument and it may return fn
// map, filter, reduce are HOF's
function returnFn() {
return function () {
console.log("inner fn")
}
}
const fn = returnFn();
fn();
// pure function
// is a fn it's produce same output as a input
function welcome(name) {
console.log(`Hi ${name}`)
}
welcome("kalidas");
// im pure function
// is a fn it's not produce same output as a input
let msg = "hai";
function welcome1(name) {
console.log(`${msg} ${name}`)
}
welcome1("kalidas"); // hai kalidas
msg = "welcome";
welcome1("kalidas"); // welcome kalidas
// IIFE (Immediately Invoked Function Expression)
(function () {
console.log("IIFE fn")
})();
// recursion
// fn that refer or call itself with base condition
/* function fn(){
if(condition) return
console.log("---fn---")
fn();
}
fn()
const f1=function f2(){
if(condition) return
f1()
}
f1()
*/
function fetchWater(count){
if(count===0){
console.log("no more water")
return
}
console.log("fetching water...")
fetchWater(count-1)
}
fetchWater(4);