JavaScript Tutorials

JavaScript String includes()
Syntax & Examples

String.includes() method

The includes() method of the String class in JavaScript determines whether the calling string contains the characters of a specified string, searchString. An optional parameter, position, can specify the position within the string at which to begin the search.


Syntax of String.includes()

There are 2 variations for the syntax of String.includes() method. They are:

1.
includes(searchString)

Parameters

ParameterOptional/RequiredDescription
searchStringrequiredThe characters to be searched for within this string.

This method determines whether the calling string contains searchString.

Returns value of type Boolean.

2.
includes(searchString, position)

Parameters

ParameterOptional/RequiredDescription
searchStringrequiredThe characters to be searched for within this string.
positionoptionalThe position within the string at which to begin the search. Defaults to 0.

This method determines whether the calling string contains searchString, starting the search at the specified position.

Returns value of type Boolean.



✐ Examples

1 Using includes() method to check if a string contains a substring

In JavaScript, we can use the includes() method to check if a string contains a specified substring.

For example,

  1. We define a string variable str with the value 'Hello, world!'.
  2. We use the includes() method with the argument 'world' to check if the string contains 'world'.
  3. The result is stored in the variable result.
  4. We log result to the console using the console.log() method.

JavaScript Program

const str = 'Hello, world!';
const result = str.includes('world');
console.log(result);

Output

true

2 Using includes() method with a specified start position

In JavaScript, we can use the includes() method to check if a string contains a specified substring, starting from a given position.

For example,

  1. We define a string variable str with the value 'Hello, world!'.
  2. We use the includes() method with the arguments 'Hello' and 5 to check if the substring from position 5 contains 'Hello'.
  3. The result is stored in the variable result.
  4. We log result to the console using the console.log() method.

JavaScript Program

const str = 'Hello, world!';
const result = str.includes('Hello', 5);
console.log(result);

Output

false

3 Using includes() method to check a non-existent substring

In JavaScript, we can use the includes() method to check if a string does not contain a specified substring.

For example,

  1. We define a string variable str with the value 'Hello, world!'.
  2. We use the includes() method with the argument 'JavaScript' to check if the string contains 'JavaScript'.
  3. The result is stored in the variable result.
  4. We log result to the console using the console.log() method.

JavaScript Program

const str = 'Hello, world!';
const result = str.includes('JavaScript');
console.log(result);

Output

false

Summary

In this JavaScript tutorial, we learned about includes() method of String: the syntax and few working examples with output and detailed explanation for each example.