Learn about how to use the String.includes() method in JavaScript.

The includes() method determines whether one string has another string inside of it. And returns a true or false value as appropriate.

Here are some code examples:

      const str = "Taco Bell is very good!"

      str.includes("Taco Bell is very good!")  //true
      str.includes("Taco Bell")                // true
      str.includes("good")                     // true
      str.includes("Taco Johns")               // false
      str.includes("TACO BELL")                // false
      str.includes("taco Bell")                // false
      str.includes(" ")                        // true

Notice that the includes() method is case sensitive and that it picks up on spaces as well.

Build A Hacker News Clone
Learn how to build and deploy a full-stack, production-ready application with React.js, Node.js, and MongoDB.
View Course

There is also an optional second paramater to the includes() method that takes an index position integer. This serves as the position within the string at which to begin searching. It's default value is 0.

Here are a few examples of using it:

      const str = "Taco Bell is very good!"

      str.includes("aco Bell is very good!", 1)  // true
      str.includes("aco Bell is very good!", 2)  // false
      str.includes("good", 8)                    // true
      str.includes("Taco", 8)                    // false

Check out the Mozilla Developer Documentation page for more information on this method.

Thanks for reading and happy coding!