Higher Order Function
1 min readMay 26, 2026
forEach — Used for iteration
map — Used for Transform
Get Harshit chaturvedi’s stories in your inbox
Join Medium for free to get updates from this writer.
filter — Used for filter
- forEach is the higher Order function used for iteration on array only.
- forEach kuch return ni karta
var arr = [10, 'hey', 30, false]
let run = () => {
console.log("Hello")
}
arr.forEach(run)
// Output : Hello Hello Hello Hello
var arr = ['ajay', 'bittu', 'chotu', 'dabbu']
arr.forEach(function(val) {
console.log(val)
})
// output : ajay bittu chotu dabbu
var arr = ['ajay', 'bittu', 'chotu', 'dabbu']
arr.forEach(function(element, index) {
console.log(index)
})
// Output : 0 1 2 3
var arr = ['ajay', 'bittu', 'chotu', 'dabbu']
arr.forEach(function(index, element) {
console.log(index)
})
// output : ajay bittu chotu dabbu
// yahan hme elements milengey not index, because first is always element
// weather name should be anything and second prop is index always.// Sum of Array Element
var arr = [10,20,30,40]
var sum = 0
arr.forEach(function(elem) {
sum = sum + elem
})
console.log(sum)
// Output : 100// Transform the array and find the square of each element
var arr = [10,20,30,40]
var arr2 = arr.map(function(elem){
return elem * elem
})
console.log(arr2);
// filter
var arr = [5,9,2,4,33,8]
var arr2 = arr.filter(function(elem) {
return elem > 0
})
console.log(arr2)var arr = ["Aman", "Basundi", "chintu", "dev"]
// Iteration - forEach
var out = arr.forEach(function(elem){
console.log(out)
})
// Trasformation
var out1 = arr.map(function() {
return elem + 'Welcome'
})
var arr = ['aman', 'ajay', 'anju', 'rohit', 'kiran', 'akash']
arr.filter(function(elem){
return elem.startsWith('a')
})




