String includes() Method Parameters
The includes() method in JavaScript accepts two parameters that help specify the search within a string more accurately:
searchString: This is the string segment that you are looking for within the main string. It's a required parameter, meaning you must provide a value when you call the method. For instance, if your string is "Learning JavaScript is fun", and you're checking for the presence of "fun", you'll use it like this:
"Learning JavaScript is fun".includes("fun") // This will return true
position (optional): This integer value determines where in the string to start the search. The default value is 0, which means the search starts at the beginning of the string. If you set this parameter to 10, the search would start from the 11th character (counting starts at 0). This is useful for skipping certain parts of the string if you already know they don't contain the searchString. For example:
"Learning JavaScript is fun".includes("JavaScript", 10) // This will return false because it starts searching after "JavaScript"
By adjusting these parameters, you can control where and what the includes() method searches within a string, making it a versatile tool for text processing in JavaScript.
String includes() Method Return Value
The return value of the includes() method is very straightforward: it gives you a Boolean value, which means it can only be either true or false.
-
true: You will receive this result if the searchString you are looking for is found anywhere within the main string. It means that the sequence of characters matches part of the main string, starting from the position you specified (or from the beginning if no position was specified).
- false: This result occurs if the searchString is not found in the main string from the specified position onward. It indicates that the characters you're looking for do not appear in the sequence or order you expected, or they are simply not present.
For example:
"Hello, world!".includes("world") // returns true
"Hello, world!".includes("bye") // returns false
This method is particularly useful because it provides a quick & easy way to check for the presence of a substring without having to write complex logic or loops. This makes your code cleaner & more efficient, especially in scenarios where you just need to verify the existence of text.
JavScript Contains() Method Example
Understanding how to use the includes() method can greatly enhance how you handle text in JavaScript. Here are several practical examples to illustrate different use cases and enhance your mastery of this method:
Example 1: Basic Usage
To check if a phrase contains a specific word, you might do something like this:
const message = "Search inside this string";
const wordToFind = "inside";
// Check if the message contains the word "inside"
const result = message.includes(wordToFind);
console.log(result); // This will log true
Example 2: Using the Position Parameter
Sometimes, you might want to start searching from a specific part of the string, not just the beginning. Here’s how you can do it:
const story = "Once upon a time, in a faraway land";
const searchTerm = "time";
// Start searching from the 10th character
const searchResult = story.includes(searchTerm, 10);
console.log(searchResult); // This will log false because "time" occurs before the 10th character
Example 3: Case Sensitivity
The includes() method is case-sensitive, meaning "hello" and "Hello" are considered different. Here’s an example:
const greeting = "Hello, how are you?";
// Check for a lowercase "hello"
const checkLowerCase = greeting.includes("hello");
console.log(checkLowerCase); // This will log false
// Proper way to handle case sensitivity
const checkProperCase = greeting.includes("Hello");
console.log(checkProperCase); // This will log true
Frequently Asked Questions
Can the includes() method be used on arrays?
No, the includes() method is specifically for strings. For arrays, JavaScript provides an includes() method under the Array prototype, which works similarly but checks for the presence of an element in an array.
Is the includes() method case sensitive?
Yes, the includes() method is case sensitive. To perform a case-insensitive search, you must either convert the string to all upper or lower case before calling includes().
What happens if the searchString is an empty string?
If the searchString is an empty string, the includes() method always returns true, because every string contains the empty substring by definition.
Conclusion
In this article, we have learned how the includes() method functions within JavaScript to search for substrings within a string. We discussed its syntax, parameters, how it returns results, and viewed practical examples to see it in action. We also learned how this method enhances your ability to handle text processing tasks in your coding projects, making your JavaScript code more effective and easier to maintain.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry.