Functions are objects so they can be passed as arguments into other functions. It’s always recommended to check if passed parameter is a function.
In the example below getName is passed as an argument to sayHello function. Calling sayHello(getName) outputs “Hello Mike”.
function getName() { return 'Mike' } function sayHello(getName) { if (typeof getName === 'function') { // "function" return `Hello ${getName()}` //Invoke function getName() } else { return console.log('Please pass a function'); } } sayHello(getName) // "Hello Mike" let name = "Brian"; sayHello(name) // "Please pass a function"
See the Pen Callback pattern by Mike (@mikemoney) on CodePen.