xxxxxxxxxx
function caller(otherFunction) {
otherFunction(2);
}
caller(function(x) {
console.log(x);
});
xxxxxxxxxx
setTimeout(function () {
console.log('Execute later after 1 second')
}, 1000);Code language: JavaScript (javascript)
xxxxxxxxxx
/*
Anonymous function = a function that doesn't need a name, you only need it for the
purpose of a single callback
*/
// e.g.
const arr = [1,2,3,4]
arr.map(function square(num){
return num * 2
})
// the 'square' function is only used within .map here. So it can be treated as an
// anonymous function. Short hand for an anonymous function can look like this:
arr.map(function(num){
return num * 2
})
// Or using arrow notation it can look like this:
arr.map((num) => {
return num * 2
})
xxxxxxxxxx
let printName = function (name) {
console.log('Hello ',name);
};
printName('Chandler Bing');
xxxxxxxxxx
let text = function () {
console.log('Hello World');
};
text();
xxxxxxxxxx
setTimeout(function () {
console.log('Welcome to JavaScript world')
}, 1000);