Get to know MDN better
This page was translated from English by the community. Learn more and join the MDN Web Docs community.
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015년 7월.
Array 인스턴스의 lastIndexOf() 메서드는 배열에서 특정 요소를 찾을 수 있는 마지막 인덱스를 반환하거나, 해당 요소가 없으면 -1을 반환합니다. 배열은 fromIndex에서 시작하여 역방향으로 검색됩니다.
배열에서 찾을 요소.
fromIndex Optional역방향 검색을 시작하는 0부터 시작하는 인덱스로, 정수로 변환됩니다.
배열에 있는 searchElement의 마지막 인덱스입니다. 찾을 수 없으면 -1입니다.
lastIndexOf() 메서드는 엄격한 동등성(=== 연산자에서 사용하는 것과 동일한 알고리즘)을 사용하여 searchElement를 배열 요소와 비교합니다. 두 개의 NaN 값은 동일한 것으로 비교되지 않으므로 searchElement가 NaN인 경우, lastIndexOf()는 항상 -1을 반환합니다.
다음 예제에서는 lastIndexOf()를 사용하여 배열에서 값을 찾습니다.
NaN을 찾기위해 lastIndexOf()을 사용할 수 없습니다.
다음 예제에서는 lastIndexOf를 사용하여 주어진 배열에 있는 요소의 모든 인덱스를 찾고, 발견된 요소를 다른 배열에 추가하기 위해 push()를 사용합니다.
여기서는 idx === 0인 경우를 별도로 처리해야 합니다. 요소가 배열의 첫 번째 요소인 경우 fromIndex 매개변수에 관계없이 항상 검색되기 때문입니다. 이는 indexOf() 메서드와 다릅니다.
배열의 빈 슬롯을 검색하기 위해 lastIndexOf()를 사용할 수 없습니다.
lastIndexOf() 메서드는 this의 length 속성을 읽은 다음 키가 length보다 작은 음이 아닌 정수인 각 속성에 접근합니다.
| ECMAScript® 2027 Language Specification # sec-array.prototype.lastindexof |
Enable JavaScript to view this browser compatibility table.
This page was last modified on 2025년 10월 10일 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.