The includes() method in JavaScript is used to determine whether a string contains another string or not. It returns 'true' if the string contains the specified substring, and 'false' otherwise.
Syntax: string.includes(searchString, position)
- searchString: The substring to search for within the string.
- position (optional): The position within the string at which to begin the search. If omitted, the search starts at index 0.
Example:
let str = "Hello, World!";
let includesWorld = str.includes("World");
console.log(includesWorld); // Output: true
Explanation:
- We have a string str containing "Hello, World!".
- We use includes("World") to check if the string contains the substring "World".
- The method returns 'true' because "World" is found within the string.
The includes() method is case-sensitive. If we want a case-insensitive search, we can convert both the string and the substring to lowercase or uppercase before using includes().
Code Compiler Link: https://app.singhteekam.in/codecompiler/