String includes() method in JavaScript

Explore what is string includes() method in JavaScript and how to use it.

Keep reading to unlock the potential of JavaScript string includes() method.

Are you ready to discover the power of string includes() method in JS?

Let’s start to dive into this comprehensive guide that illuminates how to determine if a string contains a specified substring.

What is JavaScript string includes() method?

The includes() method in JavaScript is a part of the String.prototype and is used to determine whether one string may be found within another string.

Its purpose is to check if a certain sequence of characters, or a “substring”, appears within the string you’re working with.

It provides a boolean outcome: true if the given string is detected, and false if it’s not found.

Syntax

string.includes(searchString, startingposition)

Parameters

searchString (Required)

The sequence of characters or string you’re looking for.

startingPosition (Optional)

This is the position from where you can specify at which character position in your string to start the search. If you don’t provide this, the search starts from the beginning of the string.

Return value

The includes() method will then return either true or false. It returns true if the substring is found within your string, and false if it’s not.

One important thing to note is that this method is case-sensitive. This means it treats lowercase and uppercase letters as different characters.

For instance, if your string is “Itsourcecode” and you check if it includes “itsourcecode,” it will return false because of the difference in case.

Supported browser

✔Chrome

✔Edge

✔Firefox

✔Opera

✔Safari

How to use the includes() method in JavaScript?

The includes() method in JavaScript is used to check if a string contains a specified string. It returns a boolean value – true if the string is found, otherwise false.

Here’s how you can use it:

let samplestr = "Hi, Welcome to Itsourcecode!";
console.log(samplestr.includes("Hi"));

In this example, includes() is used to check if the string “Hi” is included in the string “Hi, Welcome to Itsourcode.”

Since “Hi” is indeed part of “Hi, Welcome to Itsourcecode,” the method returns:

true

You can also specify a second argument to includes(), which is the position in the string at which to start searching:

let samplestr = "Hi, Welcome to Itsourcecode!";
console.log(samplestr.includes("Itsourcecode", 15)); ✅ // Outputs: true
console.log(samplestr.includes("Itsourcecode", 17)); ✅// Outputs: false

In the first console log, includes() starts searching from position 15 and finds “Itsourcecode.”

In the second console log, it starts searching from position 4 and does not find “Itsourcecode,” so it returns false.

Remember that includes() is case-sensitive. For example, “Itsourcecode”.includes(“itsourcecode”) would return false.

If you want to perform a case-insensitive search, you can convert both strings to lower case:

let samplestr = "Hi, Welcome to Itsourcecode!";
console.log(samplestr.toLowerCase().includes("itsourcecode".toLowerCase())); 

In this example, both the original string and the search string are converted to lowercase before calling includes(), so it correctly finds “Itsourcecode” in “Hi, Welcome to Itsourcecode!” and returns true.

Here’s another example of how to use the includes() method:

const samplestr = "Hi, Welcome to Itsourcode!.";

console.log(samplestr.includes("to")); // Outputs: true

console.log(samplestr.includes("Welcome")); // Outputs: true

console.log(samplestr.includes("Source")); // Outputs: false

console.log(samplestr.includes("Hi", 1)); // Outputs: false

console.log(samplestr.includes("ITSOURCECODE")); // Outputs: false

console.log(samplestr.includes("")); // Outputs: true

Complete output:

true
true
false
false
false
true

Frequently Asked Questions (FAQs)

Is includes() a string method in JavaScript?

Yes, includes() is indeed a method that you can use with strings in JavaScript. It’s used to check if a certain string, the one you’re looking for, is found within another string.

It’s case-sensitive, which means it treats uppercase and lowercase letters as different characters.

If it finds the string you’re looking for, it returns true. If it doesn’t find it, it returns false.

Can I use JS includes?

Absolutely! The includes() method is a standard part of JavaScript and you can use it in any modern web browser, including Chrome, Firefox, Safari, Edge, and Opera.

However, if you’re using Internet Explorer 11 or an earlier version, please note that includes() is not supported.

What is include and contains in JavaScript?

The includes() is a versatile method that works with both strings and arrays in JavaScript.

When used with a string, it checks if the string contains a certain substring.

When used with an array, it checks if the array contains a certain element. As for contains(), there’s no such method in JavaScript.

If you want to check if a string contains a certain substring or if an array contains a certain element, you would use includes().

Conclusion

In conclusion, the includes() method in JavaScript is used to check if a string contains a specified substring, returning a boolean value based on the result.

This method is case-sensitive and can start searching from a specified position in the string. It’s supported in all modern browsers and is an essential part of string manipulation in JavaScript.

We are hoping that this article provides you with enough information that help you understand the string includes JavaScript.

If you want to dive into more JavaScript topics, check out the following articles:

Thank you for reading Itsourcecoders 😊.

Leave a Comment