Files
2nd/10_Wiki/Topic_Programming/Topic_JavaScript/JavaScript_Array_Search.md
T
Antigravity Agent e9cbf23ab5 docs(10_Wiki): Dev 폴더 누락분 반영 — Topic_Programming으로 통합
이전 재구성 작업에서 Dev/ 폴더가 누락되었던 것을 반영.

- Dev/Topic_Programming(중첩 폴더, 78개)은 Dev 자체 최상위 폴더들
  (Architecture/Conventions/Engineering_Intelligence 등)과 완전 중복이라 제거.
- Dev 최상위 엔지니어링 지식 폴더(Architecture/Conventions/Engineering_Intelligence/
  Failure_Library/Generalized_Principles/Language/Pattern_Catalog/Platform_Guides/
  Subsystems, 77개)는 이미 Topic_Programming/Topic_Programming에 더 최신 버전이
  존재해 중복 제거(고유 콘텐츠 1개는 예외 처리하여 이동 보존).
- Dev의 W3Schools 언어 튜토리얼 폴더(Topic_C/CPP/CSS/CSharp/HOWTO/HTML/Java/
  JavaScript/PHP/Python/SQL/W3CSS, 1201개)는 전부 Topic_Programming 하위로 이동.
- 에이전트 운영 상태(.astra/docs)는 그대로 유지, 콘텐츠 폴더만 정리.
- Topic_Programming 최종 문서 수: 2784 → 3985.
2026-07-05 00:39:13 +09:00

7.6 KiB
Raw Blame History

id, title, category, status, verification_status, canonical_id, aliases, duplicate_of, source_trust_level, confidence_score, created_at, updated_at, review_reason, merge_history, tags, raw_sources, applied_in, github_commit
id title category status verification_status canonical_id aliases duplicate_of source_trust_level confidence_score created_at updated_at review_reason merge_history tags raw_sources applied_in github_commit
javascript-array-search JavaScript Array Search Frontend draft conceptual
Array search
indexOf
find array
findIndex
includes array
findLast
B 0.89 2026-06-23 2026-06-23
javascript
js
web
frontend
w3schools
array
search
https://www.w3schools.com/js/js_array_search.asp

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:

array.indexOf(item, start)

Search an array for the item "Apple":

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:

array.lastIndexOf(item, start)

Search an array for the item "Apple":

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]

const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.includes("Mango"); // is true

Syntax:

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]

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]

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]

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]

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 ES2016ES2023 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:

const fruits = ["Apple", "Orange", "Apple", "Mango"];
let position = fruits.indexOf("Apple") + 1;

Check existence:

const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.includes("Mango"); // is true

Find first element by condition:

const numbers = [4, 9, 16, 25, 29];
let first = numbers.find((value, index, array) => value > 18);

Find last element by condition (ES2023):

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)

📚 출처 (Sources)

📝 변경 이력 (Change history)

  • 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript Array Search" page (Astra wiki-curation, P-Reinforce v3.1 format).