🚨 Time is Running Out: Reserve Your Spot in the Lucky Draw & Claim Rewards! START NOW

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.

DataCamp
Pros
  • Easy to use with a learn-by-doing approach
  • Offers quality content
  • Gamified in-browser coding experience
  • The price matches the quality
  • Suitable for learners ranging from beginner to advanced
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable
Udacity
Pros
  • Simplistic design (no unnecessary information)
  • High-quality courses (even the free ones)
  • Variety of features
Main Features
  • Nanodegree programs
  • Suitable for enterprises
  • Paid Certificates of completion
Udemy
Pros
  • Easy to navigate
  • No technical issues
  • Seems to care about its users
Main Features
  • Huge variety of courses
  • 30-day refund policy
  • Free certificates of completion

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