Created
January 5, 2022 14:42
-
-
Save subodhkumar/88b3973532393b83533dd881677d721d to your computer and use it in GitHub Desktop.
First-class function in js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* #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