DEV Community

Adrian Matei for Codever

Posted on • Updated on

Higher order functions and closures example in Javascript

First with "normal" functions:

//closures and higher order function
function salute(salutation) {
  return function(firstName) {
    return function(lastName) {
      console.log(`hi ${salutation} ${firstName} ${lastName}`)


hi Mr. John Wick

Enter fullscreen mode Exit fullscreen mode

The shorter variant with arrow functions:

const saluteArrowFunction = (salutation) => (firstName) => (lastName) => console.log(`hi ${salutation} ${firstName} ${lastName}`);

saluteArrowFunction ('Mr.')('Johnny')('Cage')

hi Mr. Johnny Cage
Enter fullscreen mode Exit fullscreen mode

Learn more about:

Shared with ❤️ from Codever.   👉   use the copy to mine functionality to add it to your personal snippets collection.

Discussion (0)