What are Higher Order Functions in JavaScript?
A Thread๐งต๐๐ป
A Thread๐งต๐๐ป
๐๐ก๐๐ญ ๐๐ซ๐ ๐๐ข๐ ๐ก๐๐ซ ๐๐ซ๐๐๐ซ ๐
๐ฎ๐ง๐๐ญ๐ข๐จ๐ง๐ฌ ?
โ Normally when you hear the word functions you think of a regular function that takes an input, does some magic, and returns some value as output like this.
๐๐ฑ๐๐ฆ๐ฉ๐ฅ๐:
โ Normally when you hear the word functions you think of a regular function that takes an input, does some magic, and returns some value as output like this.
๐๐ฑ๐๐ฆ๐ฉ๐ฅ๐:
Now what's the difference between that normal function and a higher order function ?
โ A higher order function is a function that takes in a function as an argument or returns a function.
The functions that operate with another function are known as Higher order Functions.
โ A higher order function is a function that takes in a function as an argument or returns a function.
The functions that operate with another function are known as Higher order Functions.
Examples:
โ ๐ฆ๐๐ฉ()
โ ๐๐จ๐ซ๐๐๐๐ก()
โ ๐ซ๐๐๐ฎ๐๐()
โ ๐๐ข๐ฅ๐ญ๐๐ซ()
โ ๐ฆ๐๐ฉ()
โ ๐๐จ๐ซ๐๐๐๐ก()
โ ๐ซ๐๐๐ฎ๐๐()
โ ๐๐ข๐ฅ๐ญ๐๐ซ()
โ Now all these higher order functions apply to arrays so we'll make a dummy array with some numbers to practice using those functions.
nums = [2,4,6,8,3,5]
nums = [2,4,6,8,3,5]
๐: ๐๐๐ ๐๐๐() ๐๐๐๐๐๐
โ This method iterates over an array and takes in one parameter that represents the element in the current iteration.
โ This method iterates over an array and takes in one parameter that represents the element in the current iteration.
โ You don't need to say return as the function returns a value by default.
๐: ๐๐๐ ๐๐๐๐๐๐ ๐๐๐๐๐๐
โ This method takes a callback that takes two parameters, one that represents the element from the last iteration and the other is the current element of the iteration.
โ This method takes a callback that takes two parameters, one that represents the element from the last iteration and the other is the current element of the iteration.
4: ๐๐๐ ๐
๐๐๐๐๐() ๐๐๐๐๐๐
โ We use this method to filter out desired elements as per a condition we specify.
โ let say you only want the odd numbers or even numbers or want numbers higher or lower than a specific value,
โ We use this method to filter out desired elements as per a condition we specify.
โ let say you only want the odd numbers or even numbers or want numbers higher or lower than a specific value,
That's a wrap!
If You liked this thread.
1. Follow @Div_pradeep for more such content around web development, resources, tips, and cheatsheet.
2. RT the first tweet to share it with other people.
I appreciate it!
Thanks for reading.
If You liked this thread.
1. Follow @Div_pradeep for more such content around web development, resources, tips, and cheatsheet.
2. RT the first tweet to share it with other people.
I appreciate it!
Thanks for reading.
Loading suggestions...