JavaScript(JS) string method - includes
The includes()
method is a string method in JavaScript that is used to check whether a given string contains a specified substring or not.
Here is the syntax for the includes()
method:
str.includes(searchString[, position])
Here, str
is the original string you want to search in, and searchString
is the substring you want to search for. The optional position
parameter specifies the position within the str
to start the search from, and if not specified, it starts from the beginning of the string.
The includes()
method returns a boolean value true
if the string contains the specified substring, and false
otherwise.
Here is an example of using includes()
method:
let str = "Hello World"; console.log(str.includes("World")); // true console.log(str.includes("o")); // true console.log(str.includes("o", 5)); // false
In the first console.log()
, the includes()
method returns true
because the str
contains the substring "World". In the second console.log()
, it returns true
because the str
contains the substring "o". In the third console.log()
, it returns false
because the str
does not contain the substring "o" starting from the 5th position of the string.