Javascript Arrays

- Sort array using Array.sort()
- Minimum value from Array
- Maximum value from Array
- Sum of array values
- Create array with blank values
- Remove empty, false, undefined from array
- Remove duplicate values from array
- Combine multiple Arrays
- Check if value is an array
- Array Destructuring in Javascript(ES6)
- Remove falsy value from array
- Shuffle an array
- Remove item from Array
- Array.join() function

Javascript Objects

Javascript Conversions

Javascript Date

Javascript Cookies

To get the sum of array values which are integer type we can use .reduce() method of Javascript array to get this with one line of code.

```
const myarr = [10, 23, 32, 65, 7, 24];
const sum_of_array = myarr.reduce((a, b) => a + b);
console.log(sum_of_array);
```

**Output**

`161`

We are getting the sum of "**myarr"** variable in the code snippet which contains 6 integer values. We are using **.reduce() **array method and adding its parameters.

The reduce() function is a powerful tool for manipulating arrays in JavaScript. It can be used to quickly and easily calculate the sum of all the values in an array.

This article will explain how to use the **reduce()**** **function to compute the sum of an array of values. It will also provide some examples to illustrate how reduce() function works.

**Syntax**

`array.reduce((a, b) => a + b)`

**Example**

```
const numbers = [1,2,3,4,5];
const sum = numbers.reduce((total, num) => total + num);
console.log(sum); //Output: 15
```

This code uses the **reduce() **method to sum up all of the numbers in the array '**numbers**'. The first argument of **reduce() **is a callback function that takes two parameters: the first parameter, '**total**', is the total sum of all of the numbers in the array, and the second parameter, 'num', is the current number in the array that is being added to the total.

The initial value of the total is **0**. After the loop executes, the total sum of all of the numbers in the array is stored in the variable '**sum**', and is logged to the console with **console.log()**.

Was this helpful?

## 0 Comments