ay = Array.from(new Set(array));
    

  * Eliminating or adding elements in an array. Use [ ` flatMap() ` ](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap) instead. 

js

    
        // Takes an array of numbers and splits perfect squares into its square roots
    const roots = array.reduce((acc, cur) => {
      if (cur < 0) return acc;
      const root = Math.sqrt(cur);
      if (Number.isInteger(root)) return [...acc, root, root];
      return [...acc, cur];
    }, []);
    

js

    
        const roots = array.flatMap((val) => {
      if (val < 0) return [];
      const root = Math.sqrt(val);
      if (Number.isInteger(root)) return [root, root];
      return [val];
    });
    

If you are only eliminating elements from an array, you also can use [ `
filter() ` ](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)
.

  * Searching for elements or testing if elements satisfy a condition. Use [ ` find() ` ](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find) and [ ` findIndex() ` ](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find) , or [ ` some() ` ](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some) and [ ` every() ` ](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every) instead. These methods have the additional benefit that they return as soon as the result is certain, without iterating the entire array. 

js

    
        const allEven = array.reduce((acc, cur) => acc && cur % 2 === 0, true);
    

js

    
        const allEven = array.every((val) => val % 2 === 0);
    

In cases where ` reduce() ` is the best choice, documentation and semantic
variable naming can help mitigate readability drawbacks.

##  Specifications

Specification  
---  
[ ECMAScript Language Specification  
#  sec-array.prototype.reduce  ](https://tc39.es/ecma262/multipage/indexed-
collections.html#sec-array.prototype.reduce)  
  
##  Browser compatibility

BCD tables only load in the browser  with JavaScript enabled. Enable
JavaScript to view data.

##  See also

  * [ Polyfill of ` Array.prototype.reduce ` in ` core-js ` ](https://github.com/zloirock/core-js#ecmascript-array)
  * [ Indexed collections ](/en-US/docs/Web/JavaScript/Guide/Indexed_collections) guide 
  * [ ` Array ` ](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)
  * [ ` Array.prototype.map() ` ](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)
  * [ ` Array.prototype.flat() ` ](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat)
  * [ ` Array.prototype.flatMap() ` ](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap)
  * [ ` Array.prototype.reduceRight() ` ](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduceRight)
  * [ ` TypedArray.prototype.reduce() ` ](/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/reduce)
  * [ ` Object.groupBy() ` ](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/groupBy)
  * [ ` Map.groupBy() ` ](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/groupBy)

##  Help improve MDN

Was this page helpful to you?

Yes  No

[ Learn how to contribute
](https://github.com/mdn/content/blob/main/CONTRIBUTING.md "This will take you
to our contribution guidelines on GitHub.") .

This page was last modified on  Dec 11, 2023  by  [ MDN contributors ](/en-
US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce/contributors.txt)
.

[ View this page on GitHub
](https://github.com/mdn/content/blob/main/files/en-
us/web/javascript/reference/global_objects/array/reduce/index.md?plain=1
"Folder: en-us/web/javascript/reference/global_objects/array/reduce \(Opens in
a new tab\)") â¢  [ Report a problem with this content
](https://github.com/mdn/content/issues/new?template=page-report.yml&mdn-
url=https%3A%2F%2Fdeveloper.mozilla.org%2Fen-
US%2Fdocs%2FWeb%2FJavaScript%2FReference%2FGlobal_Objects%2FArray%2Freduce&metadata=%3C%21--+Do+not+make+changes+below+this+line+--%3E%0A%3Cdetails%3E%0A%3C