Cyber Week

Save big!

All courses under $5 - for a limited time!

Code has been added to clipboard!

string.includes

Reading time 3 min
Published Sep 8, 2017
Updated Oct 2, 2019

JavaScript String Contains: Main Tips

  • This method is used to determine whether a string contains characters of a specified string or not.
  • It returns a boolean value and is case sensitive.

includes() Function Explained

The JavaScript includes() function is used to check what a JavaScript string contains. Therefore, using this method saves you time when it is necessary to find out whether a string contains certain characters.

Like most JavaScript commands, this method for consistently reviewing what a JavaScript string contains is case sensitive. Case sensitivity refers to the fact that uppercase and lowercase letters are treated and recognized differently. Therefore, pay attention to the capitalization when using the JavaScript includes() method to determine whether a string contains characters of a specified string or not.

Example
var str = "Hi, Mark!";
var inc = str.includes("Mark");

If you click the Try it Live button above, you may try changing the capitalization on the searched value and see for yourself how the result changes.

Standard Syntax

To find out what a JavaScript string contains, it is necessary to write the includes JavaScript command correctly. Therefore, it is essential to take a look at the way this method is written and remember the arguments that can be included in the parentheses.

string.includes(searchvalue,start);

As you can see, two arguments might be included in the parentheses. The first one is named searchvalue and has to be specified because it tells the function what it should look for in a string. Without it, you won't find out what a JavaScript string contains.

The second parameter is called start. It is optional and might not always be present in the parentheses. However, it is necessary if you want to define the place in the string to start the search process. In cases when the start argument is not included, the function automatically assumes it as 0 and starts at the beginning of the string.

Parameter Description
searchvalue String to search for in the specified string. Required.
start Position to start the search in the specified string. Optional. 0 by default

Return Value

After the includes JavaScript function performs the search needed to determine what a certain JavaScript string contains, it will produce a boolean object. It's value is either true or false.

The generated boolean object shows whether the string contained the characters you were searching to find. In other words, it answers whether searchvalue has been located in the string.

Return Value: A boolean value. True if string does contain value, false if it doesn't.
JavaScript Version: ECMAScript 6