Skip to main content Accessibility Feedback

Arrays

Array.prototype.indexOf()

Get the index of an item in an array. It returns the index of the item if it’s in the array, and -1 if it’s not.

let sandwiches = ['turkey', 'tuna', 'ham', 'pb&j'];

// returns 0
sandwiches.indexOf('turkey');

// returns 3
sandwiches.indexOf('pb&j');

// returns -1
sandwiches.indexOf('grilled cheese');

Preorder my new course on Web Components! Want to learn how to build Web Components from scratch, master best practices, and more? Preorder today and get $100 off of the launch price.


Find this useful? You can support my work by purchasing an annual membership.