The includes() Function is search for the substring and returns true if it is present and false if not.
<html>
<body>
<script>
var x = "Hello Beautiful World"
var y = x.includes("Beautiful")
if (y == true) {
document.write("The substring Beautiful is present in the String")
}
else {
document.write("The substring Beautiful is not present in the String")
}
</script>
</body>
</html>
In the above code, we have declared a String Hello Beautiful World and assigned it to a variable x.
x = "Hello Beautiful World"
-Function1.png)
And we would be searching the substring Beautiful, from the String, Hello Beautiful World.
So, we have used the includes() Function to search the substring Beautiful.
y = x.includes("Beautiful")And what includes() Function does is, goes to the String Hello Beautiful World and searches for the substring Beautiful.
-Function2.png)
And finds the substring Beautiful in the 6th position.
So it returns true.
And the if statement finds a match.
if (y == true) {
document.write("The substring Beautiful is present in the String")
}
else {
document.write("The substring Beautiful is not present in the String")
}