The includes()
method in JavaScript is used to check whether a string contains a specified substring. This method takes a single argument, which is the substring to search for in the string. If the substring is found, the includes()
method returns true
. If the substring is not found, the includes()
method returns false
.
Here is an example of how the includes()
method works:
const string1 = 'Hello, world!';
const result = string1.includes('world');
// the result will be: true
In the code example above, we first declare a string called string1
that contains the text Hello, world!
. We then use the includes()
method to check if the string1
string contains the substring world
. The includes()
method takes a single argument, which is the substring to search for in the string. In this case, we pass in the string world
as the argument to the includes()
method.
As a result of calling the includes()
method, the result
variable will be set to true
, because the string1
string does indeed contain the substring world
. If the string1
string did not contain the substring world
, the result
variable would be set to false
.
Leave a Reply