TypedArray reduce() JavaScript

The Javascript TypedArray reduce() method is used to reduce the elements of an array into a single value.

Syntax:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
array.reduce(function(total, currentValue, index, arr), initialValue)
array.reduce(function(total, currentValue, index, arr), initialValue)
array.reduce(function(total, currentValue, index, arr), initialValue)

Parameters:
total: It represents the previously returned value of the function.
currentValue: It represents the index position of the current element.
index: It represents the index position of the current element.
arr: It represents the array.
thisValue: It represents this argument for the function.

Returns:
It returns the reduced single value.

Example 1:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
var Jewels = ["DIAMOND","GOLD","PLATINUM","SILVER"];
let result =Jewels.reduce(function(a,b)
{ return a + b; });
document.write(result);
</script>
</body>
</html>
<!DOCTYPE html> <html> <body> <script type="text/javascript"> var Jewels = ["DIAMOND","GOLD","PLATINUM","SILVER"]; let result =Jewels.reduce(function(a,b) { return a + b; }); document.write(result); </script> </body> </html>
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">   
var Jewels = ["DIAMOND","GOLD","PLATINUM","SILVER"];
let result =Jewels.reduce(function(a,b)  
{  return a + b;  });   
document.write(result);  
</script>
</body>
</html>

Example 2:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
const testArray = new Uint8Array([10, 11, 12, 13]);
function sum(previousValue, currentValue) {
return previousValue + currentValue;
}
document.write(testArray.reduce(sum));
</script>
</body>
</html>
<!DOCTYPE html> <html> <body> <script type="text/javascript"> const testArray = new Uint8Array([10, 11, 12, 13]); function sum(previousValue, currentValue) { return previousValue + currentValue; } document.write(testArray.reduce(sum)); </script> </body> </html>
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript"> 
const testArray = new Uint8Array([10, 11, 12, 13]);
 
function sum(previousValue, currentValue) {
  return previousValue + currentValue;
}
 
document.write(testArray.reduce(sum));
 
</script>
</body>
</html>