DEV Community 👩‍💻👨‍💻

Fakrul Islam Robin
Fakrul Islam Robin

Posted on

The every and reduce method in javascript array

  1. every();

The every method in JavaScript aray returns the Boolean value is the every element of an array passes the functions condition.

The every method will executes the user specified function on every present element of an array until it finds falsy value. If any falsy value found by the callback function it immediately returns the false. Hence, the callback function returns truthy value for every element the every will return true.


const func = (value) => value < 30;
const array1 = [25,28,15,19,7];
console.log(array1.every(func)); //returned true
Enter fullscreen mode Exit fullscreen mode
  1. reduce()

The reduce method in JavaScript
The every method and reduce method in javascript array operation is execute a users specified callback function on each element of an array. The final result of the function will be a single value.

When first time the callback function run no value will be return of the previous calculation. If we give any initial value as starts value it will use otherwise the 0 number index element will use as an initial value and iteration starts from index number 1./

const array1 = [1, 2, 3, 4];
const reducer = (previousValue, currentValue) => previousValue + currentValue;

// 1 + 2 + 3 + 4
// 5 + 1 + 2 + 3 + 4
console.log(array1.reduce(reducer, 5));
// expected output: 15
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Advice For Junior Developers

>> Check out this classic DEV post <<