Skip to content

Instantly share code, notes, and snippets.

@asimkaya
Last active November 24, 2021 10:35
Show Gist options
  • Save asimkaya/b1684500b6a48c46536a805473a11d90 to your computer and use it in GitHub Desktop.
Save asimkaya/b1684500b6a48c46536a805473a11d90 to your computer and use it in GitHub Desktop.
javascript callback functions
//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