-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallbacks.js
More file actions
37 lines (31 loc) · 863 Bytes
/
callbacks.js
File metadata and controls
37 lines (31 loc) · 863 Bytes
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
//Callback is function that gets executed on inside a function
//! A callback function is a function passed to another function as an argument.
function callbackExample(name, callback) {
console.log(callback(name));
}
callbackExample("Rakib", function (name) {
return "Hello " + name;
});
console.log("---------------------");
setTimeout(function () {
console.log("inside timer");
}, 1000);
const outer = (callbackFn) => {
console.log("Outer Function");
callbackFn();
};
outer(function inner() {
console.log("Inner Callback Function");
});
console.log("---------------------");
function each(array, callback) {
for (let i = 0; i < array.length; i++) {
if (callback(array[i])) {
console.log(array[i]);
}
}
}
function isPositive(n) {
return n > 0;
}
each([-2, 7, 11, -4, -10], isPositive);