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}`)
}
}
}

salute(‘Mr.’…


This content originally appeared on DEV Community and was authored by Adrian Matei

First with "normal" functions:

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

salute('Mr.')('John')('Wick')

//output
hi Mr. John Wick

The shorter variant with arrow functions:

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

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

//output
hi Mr. Johnny Cage

Learn more about:

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


This content originally appeared on DEV Community and was authored by Adrian Matei


Print Share Comment Cite Upload Translate Updates
APA

Adrian Matei | Sciencx (2021-08-11T10:37:11+00:00) Higher order functions and closures example in Javascript. Retrieved from https://www.scien.cx/2021/08/11/higher-order-functions-and-closures-example-in-javascript/

MLA
" » Higher order functions and closures example in Javascript." Adrian Matei | Sciencx - Wednesday August 11, 2021, https://www.scien.cx/2021/08/11/higher-order-functions-and-closures-example-in-javascript/
HARVARD
Adrian Matei | Sciencx Wednesday August 11, 2021 » Higher order functions and closures example in Javascript., viewed ,<https://www.scien.cx/2021/08/11/higher-order-functions-and-closures-example-in-javascript/>
VANCOUVER
Adrian Matei | Sciencx - » Higher order functions and closures example in Javascript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/11/higher-order-functions-and-closures-example-in-javascript/
CHICAGO
" » Higher order functions and closures example in Javascript." Adrian Matei | Sciencx - Accessed . https://www.scien.cx/2021/08/11/higher-order-functions-and-closures-example-in-javascript/
IEEE
" » Higher order functions and closures example in Javascript." Adrian Matei | Sciencx [Online]. Available: https://www.scien.cx/2021/08/11/higher-order-functions-and-closures-example-in-javascript/. [Accessed: ]
rf:citation
» Higher order functions and closures example in Javascript | Adrian Matei | Sciencx | https://www.scien.cx/2021/08/11/higher-order-functions-and-closures-example-in-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.