findIndex()

can anyone explain findIndex in js how is the workflow in brief.
2 Replies
lko
lko4mo ago
const array1 = [5, 12, 8, 130, 44];

console.log(array1.findIndex((element) => element > 13));
// Expected output: 3
const array1 = [5, 12, 8, 130, 44];

console.log(array1.findIndex((element) => element > 13));
// Expected output: 3
.findIndex() looks inside array1, and for each element, it looks if element > 13, if it is, it returns its index
Joao
Joao4mo ago
I strongly suggest looking using MDN for referencing JavaScript documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
MDN Web Docs
Array.prototype.findIndex() - JavaScript | MDN
The findIndex() method of Array instances returns the index of the first element in an array that satisfies the provided testing function. If no elements satisfy the testing function, -1 is returned.
Want results from more Discord servers?
Add your server