In JavaScript, the includes()
method is used to determine whether a string contains another string or a specified value. It returns true
if the string contains the specified value, and false
otherwise.
Syntax:
string.includes(searchValue, startIndex)
-
string
: The string in which to search for the specified value. -
searchValue
: The value to search for within the string. -
startIndex
(optional): The index at which to start the search within the string. Default is0
.
Example: Here, the includes()
method is called on the str
string to determine whether it contains the substring “World”. Since “World” is present within the string, the method returns true
, which is then assigned to the variable containsWorld
.
Javascript
const str = "Hello, World!" ;
const containsWorld = str.includes( "World" );
console.log(containsWorld); // Output: true
|
Output
true