Last updated on Oct 31, 2021 by Suraj Sharma
indexOf method returns the index of the first matched item from a given array. However, it returns
-1 if the item is not present in the array.
const vehicles = ['Cars', 'Buses', 'Trains', 'Airplanes']; vehicles.indexOf('Bikes') // -1 vehicles.indexOf('Trains') // 2
findIndex method is not supported in older browsers. This method accepts a callback function, which returns an index of the item when the condition inside it returns
const places = ['Toronto', 'Mumbai', 'Melbourne', 'Berlin']; vehicles.findIndex(place=>place==='New York') // -1 vehicles.findIndex(place=>place==='Mumbai') // 1
Rate this post
Suraj Sharma is a Full Stack Software Engineer. He holds a B.Tech degree in Computer Science & Engineering from NIT Rourkela.