Last active
November 24, 2021 10:35
-
-
Save asimkaya/b1684500b6a48c46536a805473a11d90 to your computer and use it in GitHub Desktop.
javascript callback functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//callback | |
const callback1 = (n) => { | |
return n ** 2; | |
}; | |
function cube(callback, n) { | |
return callback1(n) * n; | |
} | |
//returning functions | |
const highOrder = (n) => { | |
const doSomeThing = (m) => { | |
const doWhatEver = (t) => { | |
return 2 * n + 3 * m + t; | |
}; | |
return doWhatEver; | |
}; | |
return doSomeThing; | |
}; | |
console.log(highOrder(2)(3)(10)); | |
//returning function with callback | |
const numbers = [1, 2, 3, 4]; | |
const sumArray = (arr) => { | |
let sum = 0; | |
const callBack = function (element) { | |
sum += element; | |
}; | |
arr.forEach(callBack); | |
return sum; | |
}; | |
console.log(sumArray(numbers)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment