-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallbacks.js
More file actions
34 lines (26 loc) · 1 KB
/
Copy pathcallbacks.js
File metadata and controls
34 lines (26 loc) · 1 KB
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
/**
* Callbacks in Java
* A callback is a function that will be executed after another function gets executed.
* Functions that are used as an argument to another function are called callback functions.
* JavaScript functions have the type of Objects. So, much like any
* other objects (String, Arrays etc.), They can be passed as an argument to any other function while calling.
*/
function divideByHalf(sum){
console.log(Math.floor(sum / 2));
}
function multiplyBy2(sum){
console.log(sum * 2);
}
function operationOnSum(num1,num2,operation){
var sum = num1 + num2;
operation(sum);
}
operationOnSum(3, 3, divideByHalf); // Outputs 3
operationOnSum(5, 5, multiplyBy2); // Outputs 20
operationOnSum(5,6,function disp(value){
document.write("Message from callback method");
document.getElementById("sometext").innerHTML = "Vinod";
console.log("Message from callback method");
console.log(value);
});
//Ref: https://www.geeksforgeeks.org/javascript-callbacks/