Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify behavior of Array.p.lastIndexOf() fromIndex arg #11731

Merged
merged 1 commit into from
Jan 5, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ lastIndexOf(searchElement, fromIndex)
- `searchElement`
- : Element to locate in the array.
- `fromIndex` {{optional_inline}}
- : The index at which to start searching backwards. Defaults to the array's length
minus one (`arr.length - 1`), i.e. the whole array will be searched. If the
index is greater than or equal to the length of the array, the whole array will be
searched. If negative, it is taken as the offset from the end of the array. Note that
even when the index is negative, the array is still searched from back to front. If
the calculated index (`arr.length + fromIndex`) is less than 0, -1 is returned, i.e. the array will not be
searched.
- : The position in the array at which to start searching backwards. Defaults to the array's length minus one (`arr.length - 1`), causing the whole array to be searched.

A `fromIndex` value greater than or equal to the length of the array also causes the whole array to be searched. (In this case, you can think of it conceptually as causing the method to start its search at a nonexistent position beyond the end of the array, but to then go backwards from there looking for the real end position of the array, at which point it starts searching backwards through the actual array elements.)

A `fromIndex` value greater than 0 is taken as the offset from the beginning of the array.

A `fromIndex` value less than 0 is taken as the offset from the end of the array — in other words, it is taken as specifying the position at `array.length + fromIndex`. Therefore, if `array.length + fromIndex` is less than 0, the array is not searched, and the method returns -1. (In this case, because `fromIndex` specifies a nonexistent position before the beginning of the array, you can think of it conceptually as causing the method to start its search at that nonexistent position and to then go backwards from there looking for array elements, which it never finds.)

### Return value

Expand All @@ -46,7 +46,7 @@ The last index of the element in the array; **-1** if not found.

`lastIndexOf` compares `searchElement` to elements of the Array
using [strict
equality](/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators#Using_the_Equality_Operators) (the same method used by the ===, or triple-equals, operator).
equality](/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality) (the same method used by the `===`, or triple-equals, operator).

## Examples

Expand Down