Table of contents
1.
Introduction
2.
String includes() Method Syntax
3.
String includes() Method Parameters
4.
String includes() Method Return Value
5.
JavScript Contains() Method Example
5.1.
Example 1: Basic Usage
5.2.
Example 2: Using the Position Parameter
5.3.
Example 3: Case Sensitivity
6.
Frequently Asked Questions
6.1.
Can the includes() method be used on arrays?
6.2.
Is the includes() method case sensitive?
6.3.
What happens if the searchString is an empty string?
7.
Conclusion
Last Updated: Aug 13, 2025
Easy

Javascript String Contains

Author Pallavi singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In JavaScript contains() method is a useful tool for checking if a string contains a specific character or substring. It helps developers quickly determine whether an element exists within a string, making it a handy feature for various string manipulation tasks. 

Javascript String Includes() Method

In this article, we'll talk about the syntax, parameters, return value & examples of the JavaScript includes() method, which will help us to understand this topic in detail.

String includes() Method Syntax

The includes() method is part of JavaScript's String object & is used to determine if a specific sequence of characters, referred to as a substring, exists within a string. The basic syntax of the includes() method is straightforward:

string.includes(searchString, position)


Here, string is the original string on which the method is called. The searchString is the text you are searching for within the string. The position is an optional argument, starting at which the method should begin searching for the searchString within the string. If position is not provided, the search starts at the beginning of the string.

For example, if you want to check if the word "code" is in the sentence "JavaScript coding basics", you would write:

"JavaScript coding basics".includes("code") // This will return true


This simple method helps in searching texts quickly & efficiently without using loops or more complex procedures.

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 DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry.

Live masterclass