Skip to main content Accessibility Feedback

Strings

String.prototype.indexOf()

Find a substring inside a string. It returns the index of where the substring starts in the string, or -1 if the substring isn’t found. It’s case-sensitive.

let str = 'I love Cape Cod potato chips.';

// Returns 7
str.indexOf('Cape Cod');

// Returns 7
str.indexOf('Cape C');

// Returns -1
str.indexOf('cape cod');

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.