> For the complete documentation index, see [llms.txt](https://april.gitbook.io/learning-js/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://april.gitbook.io/learning-js/chapter-8./8.3.md).

# 8.3 배열 검색

### 1) indexOf, lastIndexOf

* indexOf 는 찾고자 하는 것과 **정확히 일치(===)하는 첫 번째 요소의 인덱스**를 반환
* lastIndexOf는 **배열의 끝**에서부터 검색
* **검색을 시작할 인덱스 지정 가능**
* 둘 다 일치하는 것을 찾지 못하면 -1을 반환

```javascript
const o = { name: "Jerry" };
const arr [ 1, 5, "a", o, true, 5, [1, 2], "9"];

arr.indexOf(5); // 1
arr.lastIndexOf(5); // 5
arr.indexOf({ name: "Jerry" }) // -1
```

### 2) findIndex

* 일치하는 것을 찾지 못하면 -1 반환
* **보조 함수를 써서 검색 조건을 지정할 수 있음 -> indexOf보다 더 다양한 상황에서 활용 가능**
* 검색을 시작하는 인덱스를 지정 불가능
* findLastIndex 없음

```javascript
const arr = [ { id: 5, name: "Judith" }, { id: 7, name: "Francis" } ];
arr.findIndex( o => o.id === 5); // 0
arr.findIndex( o => o.name === "Francis" ) // 1
arr.findIndex( o => o === 3 ); // -1
arr.findIndex( o => o.id === 17 ); // -1
```

### 3) find

* ~~조건에 맞는 요소의 인덱스~~가 아니라 **요소 자체**를 원할 때
* 검색 조건을 함수로 전달할 수 있음
* 조건에 맞는 요소가 없는 경우? undefined 반환

```javascript
const arr = [ { id: 5, name: "Judith" }, { id: 7, name: "Francis" } ];
arr.find( o => o.id === 5 ); // 객체 { id: 5, name: "Judith" }
arr.find( o => o.id === 2 ); // undefined
```

### 4) some

* 조건에 맞는 요소를 **찾으면 즉시 검색을 멈추고** true 반환

```javascript
const arr = [ 5, 7, 12, 15, 17 ];
arr.some( x => x % 2 === 0 ); // true; 12는 짝수
arr.some( x => Number.isInteger(Math.sqrt(x))); // false; 제곱수가 없음
```

### 5) every

* 배열의 **모든 요소가 조건에 맞아야** true 반환
* 조건에 맞지 않는 요소를 찾아야만 검색을 멈추고 false 반환

```javascript
const arr = [ 4, 6, 16, 36 ];
arr.every( x => x % 2 === 0 ); // true; 홀수가 없음
arr.every( x => Number.isInteger(Math.sqrt(x))); // false; 6은 제곱수가 아님
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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.3.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.
