// simple callback
let x = function() {
console.log("this is my first function")
}
let y = function(callback) {
console.log("this is my second function");
callback();
}
y(x);
//real use of callback function
let add = function(a, b) {
return a + b;
}
let multiply = function(a, b) {
return a * b;
}
let doWatever = function(a, b) {
console.log(`my return number is ${a} , ${b}`)
}
let calc = function(num1, num2, callback) {
//checking whether the callback is function or not
if (typeof callback === 'function') {
return callback(num1, num2);
} else {
}
}
console.log(calc(2, 3, doWatever));
//OR
console.log(calc(2, 3, function(a, b) {
return a + b;
}));
//few interesting examples
let myArr = [
{value:1, name:'apple'},
{value:4, name:'cabbage'},
{value:8, name:'banana'}
]
myArr.sort(function(val1,val2){
if(val1.name < val2.name){return -1}
else {
return 1
}
})
console.log(myArr)
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.