Skip to main content Accessibility Feedback

Arrays

Array.indexOf()

Get the index of the first matching item in an array. Returns -1 if no matching item is found.

var sandwiches = ['turkey', 'tuna', 'blt', 'pb&jb'];

// returns 1
sandwiches.indexOf('tuna');

// returns -1
sandwiches.indexOf('ham');

Works in all modern browsers, and IE9+. You can extend support back further with polyfills.