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
What Is JavaScript Used For?
Tutorial
Introduction
Output
Syntax
Comment
Commands
Operators
Comparison and Logical Operators
Data Types
Math.random()
Type Conversion
Function Definitions
Events
Objects
Object Properties
Prototype
Array
Sorting Arrays
Strings
Numbers
Number Format
Math Object
Onclick Event
Date
Date Formats
Scope
Regular Expressions
Reserved Words
Common Mistakes
Performance
Forms
Form Validation
Window: The Browser Object Model
Popup Boxes
Cookies
JSON
AJAX Introduction
AJAX Form
Automatic File Download
Functions
Array Methods
String Methods
Date Methods
Timing Events
Cheat Sheet
JavaScript in HTML
HTML DOM Methods
HTML DOM Changing HTML
HTML DOM Animation
HTML DOM EventListener
HTML DOM Navigation
HTML DOM NodeList
HTML DOM Element Nodes
Array Functions
Boolean
Calling a Function
Date Functions
Global Objects
Input Text
Operator
Statements
String Functions
Math
Math.random
Number
RegEx
alert
array.filter
array.length
array.map
array.reduce
array.push
array.sort
break and continue
className
confirm
decodeURIComponent
for
forEach
if
indexOf
innerHTML
location.reload
number.toString
onclick
onload
parseInt
prompt
replace
setAttribute
setInterval
setTimeout
slice
splice
string.includes
string.indexOf
string.split
style.display
submit
substr
substring
switch
test
throw, try and catch
toLowerCase
toUpperCase
use strict
while
window.history
window.location
window.navigator
window.screen