Skip to content

Instantly share code, notes, and snippets.

@subodhkumar
Created January 5, 2022 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save subodhkumar/88b3973532393b83533dd881677d721d to your computer and use it in GitHub Desktop.
Save subodhkumar/88b3973532393b83533dd881677d721d to your computer and use it in GitHub Desktop.
First-class function in js
/* #1. Assign the function to a variable */
let functionVar = function() {
console.log('Hello World!')
}
functionVar();
/* #2. Update function variable */
functionVar = () => {
console.log(`Hello World Updated!`)
}
functionVar();
/* #3. Pass function as an argument */
let executeFunction = (fnArg)=>{
fnArg();
}
executeFunction(()=>{console.log(`Hello World from Function Argument`)}); // this will in turn execute the function passed an argument
/* #4. Returning function as return value */
let returnAFunction = ()=>{
return ()=>{
console.log(`Hello World from Function return value`)
}
}
// execute like below
returnAFunction()();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment