JavaScript – Check if String contains Specified String
To check if a string str1
contains a specified string str2
in JavaScript, call includes() method on the string str1
, and pass the specific string str2
as argument.
includes() method returns a boolean value. The return value is true
if specified string is present in this string, or else, it returns false
.
Syntax
The syntax to check if string str1
contains the string str2
is
str1.includes(str2)
We can also provide an optional position index
, from which the search for string str2
should happen in string str1
.
The syntax to check if string str1
contains the string str2
after the position index
is
str1.includes(str2, index)
Examples
The following is a quick example to check if string str1
contains the string str2
in JavaScript.
var str1 = 'An apple a day, keeps doctor away.';
var str2 = 'apple';
var result = str1.includes(str2);
We may use the return value of includes() method as a condition in If statement.
var str1 = 'An apple a day, keeps doctor away.';
var str2 = 'apple';
if ( str1.includes(str2) ) {
//str1 contains str2
} else {
//str1 does not contain str2
}
In the following example, we take two strings: str1
and str2
in script, and check if string str1
contains the string str2
using includes() method. We shall display the resulting string in a div.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<div id="output"></div>
<script>
var str1 = 'An apple a day, keeps doctor away.';
var str2 = 'apple';
if ( str1.includes(str2) ) {
displayOutput = 'str1 contains str2.';
} else {
displayOutput = 'str1 does not contain str2.';
}
document.getElementById('output').innerHTML = displayOutput;
</script>
</body>
</html>
Now, let us change the value of string str2
, such that string str2
is not present in the string str1
.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<div id="output"></div>
<script>
var str1 = 'An apple a day, keeps doctor away.';
var str2 = 'banana';
if ( str1.includes(str2) ) {
displayOutput = 'str1 contains str2.';
} else {
displayOutput = 'str1 does not contain str2.';
}
document.getElementById('output').innerHTML = displayOutput;
</script>
</body>
</html>
The following is a quick example to check if string str1
contains the string str2
from a specific position index
in JavaScript.
var str1 = 'An apple a day, keeps doctor away.';
var str2 = 'apple';
var index = 8;
if ( str1.includes(str2, index) ) {
//str1 contains str2 from specified index
} else {
//str1 does not contain str2 from specified index
}
In the following example, we take two strings: str1
and str2
in script, and check if string str1
contains the string str2
from position 8
using includes() method.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<div id="output"></div>
<script>
var str1 = 'An apple a day, keeps doctor away.';
var str2 = 'banana';
var index = 8;
if ( str1.includes(str2, index) ) {
displayOutput = 'str1 contains str2 from specified position.';
} else {
displayOutput = 'str1 does not contain str2 from specified position.';
}
document.getElementById('output').innerHTML = displayOutput;
</script>
</body>
</html>
Conclusion
In this JavaScript Tutorial, we learned how to check if a string contains another specific string in JavaScript, using includes() method, with examples.