# 8.5 배열의 마법 reduce

* **배열 자체를 변형**
* 첫 번째 매개변수 : 배열이 줄어드는 대상인 어큐물레이터
* 두 번째 매개변수부터 현재 배열 요소, 현재 인덱스, 배열 자체 순으로 받음

```javascript
const arr = [ 5, 7, 2, 4 ];
const sum = arr.reduce((a, x) => a += x);

1. 두 번째 배열 요소 7에서 함수가 호출된다.
a의 초기값은 첫 번째 배열요소인 5이고, x의 값은 7이다.
함수는 a와 x의 합인 12를 반환하고, 이 값이 다음 단계에서 a의 값이 된다.

2. 세 번째 배열 요소인 2에서 함수를 호출한다.
a의 초기값은 12이고, x의 값은 2이다.
함수는 a와 x의 합인 14를 반환한다.

3. 네 번째이자 마지막 배열 요소인 4에서 함수를 호출한다.
a는 14이고, x는 4이다.
함수는 a와 x의 합인 18을 반환하며 이 값은 reduce의 값이고, sum에 할당되는 값이다.
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://april.gitbook.io/learning-js/chapter-8./8.5-reduce.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
