
196 views
JavaScript string includes() method
The includes()
method in JavaScript is used to determine whether a string contains a specified substring. It returns true
if the substring is found within the string, and false
otherwise. Here’s the syntax:
JavaScript
string.includes(searchString, position)
searchString
: The substring to search for within the string.position
(optional): The index at which to start the search within the string. If not specified, the search starts from index 0.
The includes()
method is case-sensitive, meaning it distinguishes between uppercase and lowercase characters. Here’s an example:
JavaScript
var str = "Hello, world!";
console.log(str.includes("Hello")); // true
console.log(str.includes("hello")); // false (case-sensitive)
console.log(str.includes("world")); // true
console.log(str.includes("World")); // false (case-sensitive)
console.log(str.includes("lo")); // true
console.log(str.includes("lo", 5)); // false (search starts from index 5)
In this example, the includes()
method is called on the str
string object. It checks whether the specified substrings are present within the string and returns true
or false
accordingly.