JavaScript String includes() Tutorial

In this section, we will learn what the includes() method is and how to use it.

JavaScript String includes() Method

We use the `includes()` method to see if the target String contains a specific value or not.

For example, you might have a string like “This is a simple string” and want to see if it has the value “simple” in it. The includes() method can help you with this situation.

JavaScript String includes() Syntax

string.includes(searchvalue, start)

String includes() Method Parameters:

The `includes()` method takes two arguments:

  • The first argument is the value that we want to know if it exists in the target string or not.
  • The second value is the index number from which the search should begin. The second argument is optional and if we ignore it, the search will start from the index 0.

String includes() Method Return Value:

The return value of this method is either `true` or `false`.

If the value is `true` that means the target string value contains the value that we set as the argument to the method.

The value `false` means otherwise.

Example: using String includes() method in JavaScript

const message = "The relationship between passion and determination is one-to-many!"

console.log(message.includes("between"));

console.log(message.includes("passion"));

Output:

true

true

Leave a Reply