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:
includes(searchString)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
searchString | required | The characters to be searched for within this string. |
This method determines whether the calling string contains searchString.
Returns value of type Boolean
.
includes(searchString, position)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
searchString | required | The characters to be searched for within this string. |
position | optional | The 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,
- We define a string variable
str
with the value'Hello, world!'
. - We use the
includes()
method with the argument'world'
to check if the string contains'world'
. - The result is stored in the variable
result
. - We log
result
to the console using theconsole.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,
- We define a string variable
str
with the value'Hello, world!'
. - We use the
includes()
method with the arguments'Hello'
and5
to check if the substring from position5
contains'Hello'
. - The result is stored in the variable
result
. - We log
result
to the console using theconsole.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,
- We define a string variable
str
with the value'Hello, world!'
. - We use the
includes()
method with the argument'JavaScript'
to check if the string contains'JavaScript'
. - The result is stored in the variable
result
. - We log
result
to the console using theconsole.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.