DEV Community

Fakrul Islam Robin
Fakrul Islam Robin

Posted on

2 1

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.

Example:

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
console.log(array1.reduce(reducer));
// 5 + 1 + 2 + 3 + 4
console.log(array1.reduce(reducer, 5));
// expected output: 15
Enter fullscreen mode Exit fullscreen mode

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (0)

Image of Stellar post

🚀 Stellar Dev Diaries Series: Episode 1 is LIVE!

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

AWS Security LIVE!

Hosted by security experts, AWS Security LIVE! showcases AWS Partners tackling real-world security challenges. Join live and get your security questions answered.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️