JavaScript

JavaScript String includes/contains

The includes() method is a searching algorithm used to find a substring within a string or to find elements within an array. includes() method returns boolean values (It returns either true or false). So it can be used as the condition for an if statement or a loop.

In this how-to guide, we will learn how to use the includes() method to find a substring in a string; but first, let’s discuss the syntax of includes() in JavaScript.

Syntax:

string_name.includes(substring, starting_point)

array_name.includes(element, starting_point)

The include() method takes two parameters in JavaScript:

  1. substring/element: The first parameter is required. It is the substring/element which needs to be found within the string/array.
  2. starting_point: This parameter is optional. It gives the position at which to start the search. It is 0 by default.

Difference between includes() and contains()

The includes and contains both are searching algorithms used to find a substring within a string or find elements within an array. The includes() is a method native to JavaScript, whereas contains() is used in other languages such as Java. So from here on forward, we will only use includes() in our article.

Note: We will use the browser console to demonstrate examples performed in this post. To open up the browser console:

  • Use the F12 key in Chrome and other chromium-based browsers.
  • Use CTRL + SHIFT + K keyboard shortcut keys for Mozilla.
  • Use Option + ⌘ + C keyboard shortcut keys in Safari (if the developer menu does not appear, open Preferences by pressing ⌘ + , and in the Advanced tab, check “Show Develop menu in menu bar”).

How to find a substring in a String (JavaScript)

Now we will use the includes() method to find a substring within a string:

var str = 'Welcome to Linux Hint!';

if(str.includes('to Linux'))

{

console.log('Success');

}


In the example above, we first declared a string; and then we assigned it a value. On the next line, we used the includes() method as a condition for an if statement and passed it a string as an argument. As that string is present in the original string named str, the includes() method will return true, and the body of the if statement will be executed. If the includes method does not find the substring in the original string, then the condition would turn false, and the body of the if statement would not be executed.

We can also pass variables as an argument to the includes() method.

var str = 'Welcome to Linux Hint!';

let find = 'Linux';

if(str.includes(find))

{

console.log(`The string does contain "${find}"`);

}

The includes() method is case sensitive.

var str = 'THIS STRING IS WRITTEN IN UPPER CASE';

if(str.includes('this string is written'))
{
console.log('The required string found');
}
else
{
console.log('Could not find the required string');
}

We can also pass another parameter to the includes() method, which specifies where it will start the search.

var str = 'THIS STRING IS WRITTEN IN UPPER CASE';

if(str.includes('STRING', 5))
{
console.log('The required string found');
}
else
{
console.log('Could not find the required string');
}

var str = 'THIS STRING IS WRITTEN IN UPPER CASE';

if(str.includes('STRING', 6))
{
console.log('The required string found');
}
else
{
console.log('Could not find the required string');
}

Similarly, this method can also be used to find an element within an array.

vararr = ['Lion', 'Monkey', 'Rhino', 'Dog', 'Cat'];

if(arr.includes('Monkey'))
{
console.log('The required element found');
}
else
{
console.log('Could not find the required element');
}

Conclusion

When working with arrays or strings in a programming language, you will often need to find whether they contain a specific element or a substring. The includes() method helps us figure that out.

In this how-to guide, we have learned how to use the includes() method to find a substring/element in a string/array. Moreover, we also discussed the type and parameters we can pass to the includes() method.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.