← coderrocketfuel.com

Using The String Includes() Method With Node.js

How do you use the string.includes() method in Node.js?

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.

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

Here are a few examples of how to use 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!