DEV Community

Cover image for JavaScript: Higher-order functions Part-3
Kiran Raj R
Kiran Raj R

Posted on

JavaScript: Higher-order functions Part-3

  1. Array.prototype.map
  2. Array.prototype.filter
  3. Array.prototype.reduce
  4. Array.prototype.forEach
  5. Array.prototype.every
  6. Array.prototype.some

5. Array.portotype.every()

The every() method, return true if all the elements returns truthy for the test implemented inside the callback function, else return false. i.e. all element should return true when executed with the callback function for every() method to return "true", if at least one element cause callback to return a false value, every() method return "false". Return type of every() is Boolean. If an element fails the test in the callback function, all elements after the that element is ignored (not evaluated by callback).

//syntax
arr.every(callback(currentVal, index, array), thisArg)
Enter fullscreen mode Exit fullscreen mode

"currentVal" is the current element of the array which is being passed into the callback function. "index" is the index of the current element in the array which is an optional value. "array" is the array in which filter is executing, it's optional.
"thisArg" is the value passed to "this" of the callback during execution, if no value is provided the value will be "undefined", it is also an optional value.

let arr = [2,4,6,8,10];
let r1 = arr.every((elem)=> { 
    return elem % 2 == 0
});
console.log(r1); //True
Enter fullscreen mode Exit fullscreen mode

In the above code the callback function checks whether the element's reminder is zero when divided by two. All element return true to the test implemented by the callback function, thus the every() method returns "true".

let arr2 = [2,4,6,8,1,3,4,7];
let result_arr2 = [];
let r22 = arr2.every((elem)=> { 
    result_arr2.push(elem % 2 == 0)
    return elem % 2 == 0
});
console.log(result_arr2); 
// [true, true, true, true, false]
console.log(r22); //false
Enter fullscreen mode Exit fullscreen mode

Did you find something in the first console.log statement, I find that the output array contains only five elements, every() method should call the callback function on every element, if that was the case eight elements must have eight Boolean result in the output array so what happened? It happened because when the callback returns a false value, the every() method immediately return it's final value(here "false") and all the remaining elements are ignored.

Check the below example hope you will understand it.

let arr = [2,4,6,8,10];
let result_arr = [];
let r1 = arr.every((elem)=> { 
    result_arr.push(elem % 2 == 0)
    return elem % 2 == 0
});
console.log(result_arr); 
//[true, true, true, true, true]
console.log(r1); //True
Enter fullscreen mode Exit fullscreen mode

Another example is provided below for your reference.

let arr2 = [2,4,6,8,1,3,4,7];
let r2 = arr2.every((elem, index, array)=> { 
   console.log(`${elem} => ${index} => ${array}`);
   // 2 => 0 => 2,4,6,8,1,3,4,7
   // 4 => 1 => 2,4,6,8,1,3,4,7
   // 6 => 2 => 2,4,6,8,1,3,4,7
   // 8 => 3 => 2,4,6,8,1,3,4,7
   return elem % 2 == 0
});
console.log(r2); // False
Enter fullscreen mode Exit fullscreen mode

6. Array.prototype.some()

Some() method returns true if at least one of the element cause the callback function to return true. Same as every() method the return type of some() is also Boolean.

//syntax
arr.every(callback(currentVal, index, array), thisArg)
Enter fullscreen mode Exit fullscreen mode

"currentVal" is the current element of the array which is being passed into the callback function. "index" is the index of the current element in the array which is an optional value. "array" is the array in which filter is executing, it's optional.
"thisArg" is the value passed to "this" of the callback during execution, if no value is provided the value will be "undefined", it is also an optional value.

let arr = [2,4,6,8,1];
let r1 = arr.some((elem)=> { 
    return elem % 2 == 0
});
console.log(r1); //true
Enter fullscreen mode Exit fullscreen mode
let arr = [2,4,6,8,10];
let result_arr =[]
let r1 = arr.some((elem, index, array)=> { 
    result_arr.push(elem % 2 == 0)
    return elem % 2 == 0
});
console.log(result_arr); // [true]
console.log(r1); // true
Enter fullscreen mode Exit fullscreen mode

Look at the above code you can find that the output of first console.log statement is [true], there are 5 elements in the array but callback function only called once, why?
When the some() method is calling the callback if anyone element cause the callback to returns a true value, immediately the result is returned and all elements after the current element is ignored.

Hope you can understand the working of below code.

let r2 = arr2.some((elem, index, array)=> { 
    result_arr.push(elem % 2 == 0)
    return elem % 2 == 0
});

console.log(result_arr); 
// [false, false, false, false]
console.log(r2); // false
Enter fullscreen mode Exit fullscreen mode

One more example.

let arr2 = [1,3,41,7];
let r2 = arr2.some((elem, index, array)=> { 
   console.log(`${elem} => ${index} => ${array}`);
   // 1 => 0 => 1,3,41,7
   // 3 => 1 => 1,3,41,7
   // 41 => 2 => 1,3,41,7
   // 7 => 3 => 1,3,41,7
   return elem % 2 == 0
});
console.log(r2); // false
Enter fullscreen mode Exit fullscreen mode

Hope you understand some basic details of the higher-order functions in JavaScript. I tried my best to make everything simple. Please feel free to point out any mistakes in the articles or changes that need to be implemented.
Happy Coding.

JavaScript: Higher-order functions Part 1
JavaScript: Higher-order functions Part 2

Latest comments (0)