--- id: javascript-array-search title: "JavaScript Array Search" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["Array search", "indexOf", "find array", "findIndex", "includes array", "findLast"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.89 created_at: 2026-06-23 updated_at: 2026-06-23 review_reason: "" merge_history: [] tags: ["javascript", "js", "web", "frontend", "w3schools", "array", "search"] raw_sources: ["https://www.w3schools.com/js/js_array_search.asp"] applied_in: [] github_commit: "" --- # [[JavaScript Array Search]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) JavaScript offers a family of array search methods β€” position-based (`indexOf`, `lastIndexOf`, `includes`) and predicate-based (`find`, `findIndex`, `findLast`, `findLastIndex`) β€” that let you locate either a value's index or the first/last element matching a test. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **`indexOf()`** searches an array for a value and returns its first matching position (0-indexed), or `-1` if not found. [S1] - **`lastIndexOf()`** works like `indexOf()` but returns the position of the *last* occurrence. [S1] - **`includes()`** (ES2016) checks whether an element exists in an array and, unlike `indexOf()`, can correctly detect `NaN` values. [S1] - **`find()`** (ES6) returns the value of the first element that passes a test function. [S1] - **`findIndex()`** returns the index of the first element that passes a test function. [S1] - **`findLast()`** (ES2023) returns the value of the last element that passes a test, searching from the end. [S1] - **`findLastIndex()`** (ES2023) returns the index of the last element that passes a test. [S1] - **The callback for `find`/`findIndex`** receives three arguments: the value, the index, and the array itself. [S1] ## 🧩 μΆ”μΆœλœ νŒ¨ν„΄ (Extracted patterns) - **Position lookup pattern** β€” call `indexOf`/`lastIndexOf` and add `+ 1` to convert a 0-based index into a human-readable position. [S1] - **Existence check pattern** β€” prefer `includes()` over `indexOf() !== -1` when you only need a boolean and need `NaN` to be detectable. [S1] - **Predicate search pattern** β€” pass a test function (named or arrow) to `find`/`findIndex`/`findLast`/`findLastIndex` to locate by condition rather than by exact value. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) **indexOf()** β€” Searches an array for a value and returns its position. The first item is at position 0, the second at position 1, and so on. It returns `-1` if the item is not found. If the item is present more than once, it returns the position of the first occurrence. [S1] Syntax: ```javascript array.indexOf(item, start) ``` Search an array for the item "Apple": ```javascript const fruits = ["Apple", "Orange", "Apple", "Mango"]; let position = fruits.indexOf("Apple") + 1; ``` **lastIndexOf()** β€” `Array.lastIndexOf()` is the same as `Array.indexOf()`, but returns the position of the last occurrence of the specified element. [S1] Syntax: ```javascript array.lastIndexOf(item, start) ``` Search an array for the item "Apple": ```javascript const fruits = ["Apple", "Orange", "Apple", "Mango"]; let position = fruits.lastIndexOf("Apple") + 1; ``` **includes()** β€” ECMAScript 2016 introduced `Array.includes()` to arrays. This allows you to check if an element is present in an array (including `NaN`, unlike `indexOf`). [S1] ```javascript const fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.includes("Mango"); // is true ``` Syntax: ```javascript array.includes(search-item) ``` **find()** β€” The `find()` method returns the value of the first array element that passes a test function. This example finds (returns the value of) the first element that is larger than 18: [S1] ```javascript const numbers = [4, 9, 16, 25, 29]; let first = numbers.find(myFunction); function myFunction(value, index, array) { return value > 18; } ``` The function takes 3 arguments: the item value, the item index, and the array itself. [S1] **findIndex()** β€” The `findIndex()` method returns the index of the first array element that passes a test function. This example finds the index of the first element that is larger than 18: [S1] ```javascript const numbers = [4, 9, 16, 25, 29]; let first = numbers.findIndex(myFunction); function myFunction(value, index, array) { return value > 18; } ``` **findLast()** β€” ES2023 added the `findLast()` method that will start from the end of an array and return the value of the first element that satisfies a condition. Finding the last temperature above 40: [S1] ```javascript const temp = [27, 28, 30, 40, 42, 35, 30]; let high = temp.findLast(x => x > 40); ``` **findLastIndex()** β€” The `findLastIndex()` method finds the index of the last element that satisfies a condition. Finding the index of the last temperature above 40: [S1] ```javascript const temp = [27, 28, 30, 40, 42, 35, 30]; let pos = temp.findLastIndex(x => x > 40); ``` **Browser support** β€” The page documents browser support across Chrome, Edge, Firefox, Safari, and Opera, with most of these features fully supported since their ES2016–ES2023 introductions. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) The page's own snippets are the canonical applied examples β€” locating "Apple" in a fruits array, finding the first number over 18, and finding the last temperature above 40. No external project/commit applications found in the source. ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Find a value's position: ```javascript const fruits = ["Apple", "Orange", "Apple", "Mango"]; let position = fruits.indexOf("Apple") + 1; ``` Check existence: ```javascript const fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.includes("Mango"); // is true ``` Find first element by condition: ```javascript const numbers = [4, 9, 16, 25, 29]; let first = numbers.find((value, index, array) => value > 18); ``` Find last element by condition (ES2023): ```javascript const temp = [27, 28, 30, 40, 42, 35, 30]; let high = temp.findLast(x => x > 40); ``` ## βš–οΈ 비ꡐ 및 선택 κΈ°μ€€ (Comparison & decision criteria) - **`indexOf` vs `includes`** β€” use `indexOf`/`lastIndexOf` when you need the position; use `includes` when you need a boolean and especially when `NaN` must be detectable (which `indexOf` cannot do). [S1] - **`find`/`findIndex` vs `findLast`/`findLastIndex`** β€” the former search from the start of the array; the latter (ES2023) search from the end. Choose value-returning (`find`/`findLast`) vs index-returning (`findIndex`/`findLastIndex`) based on what you need. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) No contradictions found in the source. The source notes version provenance: `includes` is ES2016; `find`/`findIndex` are ES6; `findLast`/`findLastIndex` are ES2023. [S1] ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual (μ‹€μ œ 적용 사둀 발견 μ‹œ applied/validated둜 승격 κ°€λŠ₯) - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.89 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[JavaScript Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[JavaScript Array Sort]], [[JavaScript Array Iteration]], [[JavaScript Arrays]] - **μ°Έμ‘° λ§₯락:** Referenced whenever you need to locate elements within an array by value or by predicate. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” JavaScript Array Search β€” https://www.w3schools.com/js/js_array_search.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript Array Search" page (Astra wiki-curation, P-Reinforce v3.1 format).