FindIndex(), FindLast(), FindLastIndex(),indexOf(),lastIndexOf() in JavaScript.
FindIndex():
FindIndex(), FindLast(), FindLastIndex(),indexOf(),lastIndexOf() in JavaScript.
FindIndex():
They return the first element that matches a condition. So how like the “find” method but they return the index of element. And it didn’t modify it original array.
Direction: left → right
const find = [5, 12, 8, 130, 44];
const val = find.findIndex((i) => i > 10);
console.log(find); // [ 5, 12, 8, 130, 44 ]
console.log(val); // 1
If they didn’t match they return “-1”.
FindLast():
Return the last value of last element that satisfy or matches the condition. And also they didn’t modify it original array.
Direction: right → left
const find = [5, 12, 8, 130, 44];
const val = find.findLast((i) => i > 40);
console.log(find); // [ 5, 12, 8, 130, 44 ]
console.log(val); // 44
This one is newer (ES2023). Works in modern browsers / Node 18+
FindLastIndex():
Returns the index of the last element that matches a condition. And this also didn’t modify his original array.
Direction: right → left
const find = [1, 3, 7, 5, 3, 7];
const val = find.findLastIndex((i) => i > 3); // they gave us the index of last element that match the condition
console.log(find); // [ 1, 3, 7, 5, 3, 7 ]
console.log(val); // 5
Another example of findLastIndex()
const find = ["apple", "mango", "banana", "orange", "apple", "grapes"];
const val = find.findLastIndex((i) => i === "apple");
console.log(find); // ["apple", "mango", "banana", "orange", "apple", "grapes"];
console.log(val); // 4
In last i know you confuse about direction: You understand it from “left to right” but you confuse in “right to left” direction.
const num = [1, 2, 3, 4, 5, 6, 7];
index= 0, 1, 2, 3, 4, 5, 6; // Array indexes are fixed and never change.
// but findLastIndex() start from right to left , but index are fixed and they never change.
indexOf():
They gave us the index of first element that satisfy the condition. They didnot modify it original array.
Direction: left → right
const find = [1, 3, 7, 5, 3, 7];
const val = find.indexOf(3);
console.log(find); // [1, 3, 7, 5, 3, 7];
console.log(val); // 1
lastIndexOf: They gave us the index last element that satisfy the condition. They also didn’t modify his original array.
Direction: right → left
const find = [1, 3, 7, 5, 3, 7];
const val = find.lastIndexOf(3);
console.log(find); // [1, 3, 7, 5, 3, 7];
console.log(val); // 4
Thank you so much for your time and I hope you learn something new from this article.
메타데이터
- post_id
- c3b7afc2d3f6
- slug
- findindex-findlast-findlastindex-indexof-lastindexof-in-javascript-c3b7afc2d3f6
- url
- https://medium.com/@ali.marif/findindex-findlast-findlastindex-indexof-lastindexof-in-javascript-c3b7afc2d3f6
- canonical_url
- https://medium.com/@ali.marif/findindex-findlast-findlastindex-indexof-lastindexof-in-javascript-c3b7afc2d3f6
- author_url
- https://medium.com/@ali.marif
- status
- ok
- fetched_at
- 2026-07-17 00:47:58