• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
Actual Wizard

Actual Wizard

Great Content From Actual Wizards

  • Blogging
  • Programming
  • Social Media
  • Marketing
You are here: Home / Programming / JavaScript

JavaScript String includes Method Explained

August 29, 2020 by Kevin Marszalek

The includes() method is useful for determining if a string contains a predetermined value.

boolValue = MyString.includes("A String");

Note: This method will return a boolean value.

The example below demonstrates a typical use case for the includes() method.

var MyString = "Hello World!";
var TestString = "llo W";
var TestResult = MyString.includes(TestString);

if (TestResult){
	console.log("The String: \'" + MyString + "\' contains:\'" + TestString + "\'");
}else{
	console.log("The String: \'" + MyString + "\' does not contain:\'" + TestString + "\'");
}

The following is printed to the console:

The String: 'Hello World!' contains:'llo W'

Using the Optional Start Position Value

boolValue = MyString.includes("A String", 10);

You can optionally include a start position value with the includes() method.

It is important to know that the start position is an index value that starts at zero.

This can be confusing, as demonstrated below:

var MyString = "ABCD";
var TestString = "B";
var StartPosition = 2;

var TestResult = MyString.includes(TestString,StartPosition);
//TestResult will be false because MyString[2] == "C"

if (TestResult){
	console.log("The String: \'" + MyString + "\' contains:\'" + TestString + "\' at the start position of : " + StartPosition);
}else{
	console.log("The String: \'" + MyString + "\' does not contain:\'" + TestString + "\' at the start position of : " + StartPosition);
}

The following is printed to the console:

The String: 'ABCD' does not contain:'B' at the start position of : 2

Tip: Using a value for the start position that is larger than the length of the string does not generate a warning or an error, but the method will always return false.

Passing a Null Value As a Parameter to the includes() Method

One quirky thing to note about this method is that if you attempt to evaluate the following code:

x = MyString.includes("");

Then the value of the variable x will always be true no matter what the contents of MyString is.

Even a string that is empty will contain “” according to the includes() method.

Filed Under: JavaScript

Read More From Actual Wizard

  • Understanding the Different Parts of an Email Address
    An email address has four parts; the recipient name, the @ symbol, the domain name, and the top-level domain. …
  • How to Get the Last Element of an Array in JavaScript
    There are a number of different ways to get the last element of an array in JavaScript. Below we will explore …
  • How to use document.write() in JavaScript
    How to use document.write() in JavaScript The document.write() function is commonly used when testing simple …
  • How to Open a New Tab with JavaScript
    Opening a new browser window in a new tab can be done easily in JavaScript. Modern web browsers provide a …

Primary Sidebar

More posts from this section

  • How to Get the Last Element of an Array in JavaScript
  • How to use document.write() in JavaScript
  • How to Open a New Tab with JavaScript
  • How to Convert an HTML Node to a String in JavaScript
  • JavaScript String Anchor Method Explained
  • JavaScript String Big Method Explained
  • JavaScript String Blink Method Explained
  • JavaScript String Bold Method Explained
  • JavaScript String charAt Method Explained
  • JavaScript String charCodeAt Method Explained

Copyright © 2025 ActualWizard.com

  • About
  • Terms of Service
  • Privacy Policy
  • Cookie Policy