Starting ES6+, we can use
findIndex() to get the index of the first matched array element that satisfies the provided testing function. If the value does not exist in the array,
-1 is returned. For example:
// ES6+ const negativeNumber = (elem) => elem < 0; [101, 5, -4, 202, -4].findIndex(negativeNumber); // 2 [10, 22, 44, 0, 15].findIndex(negativeNumber); // -1
This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.