JavaScript Includes (Strings) โ€“ Simplified & Made Easy!

As easy as JavaScript is to learn, you must admit that it contains some difficult-to-understand components that may deter newcomers. However, a majority of JavaScript features like JavaScript includes() are pretty simple once you grasp them.

Apart from JavaScript includes(), other important features like JavaScript Slice and JavaScript Spread operators are also simple and easy to understand.

Letโ€™s look at the definition of includes and how to use them in programming. As always I added a for you guys so you can play around with the code below.

๐Ÿ”ฅ Learn JavaScript Programming โ€“ Beginner Friendly!
๐Ÿ”จ JavaScript Basics
๐Ÿ‘‰ JavaScript alert()
๐Ÿ‘‰ย Difference between Let and Const
๐Ÿ”ธ JavaScript Arrays
๐Ÿ‘‰ย JavaScript Array Filter
๐Ÿ‘‰ย JavaScript Array Find
๐Ÿ‘‰ JavaScript forEach (Arrays)
๐Ÿ‘‰ย JavaScript Slice (Arrays)
๐Ÿ‘‰ย JavaScript Spread Operator
๐Ÿ”ธ JavaScript Strings
๐Ÿ‘‰ย JavaScript Slice(Strings)
๐Ÿ‘‰ย JavaScript Includes
๐ŸŒ Web Development with JavaScript
๐Ÿ‘‰ย Store User Input in a variable with JS
โš™๏ธ JavaScript Specifics
๐Ÿ‘‰ย Sorting Numbers in JS
๐Ÿ‘‰ JavaScript Fetch API
๐Ÿ‘‰ toLocaleDateString in JavaScript

Table of Contents

What is JavaScript includes() for strings?

JavaScript includes() method is used to determine whether a given term or character is present in a string. If the term is present, the includes() method will send a boolean True; if not, it sends a False.

JavaScript Includes


string.includes(searchvalue, start)Code language: JavaScript (javascript)

In which,

  • The searchvalue refers to the string in which the search will occur.
  • The start relates to the position from which the search will take place. In default, the position is โ€˜0โ€™.

Itโ€™s also worth noting that the includes() method is case-sensitive. This means that if you search for uppercase characters in a string made up of lowercase characters, the return value will be โ€˜False,โ€™ even if the string is identical.

JavaScript Slice(Strings) is also an interesting concept that you should learn.

JavaScript String includes() Examples

Letโ€™s start with a simple example to show you how the includes() method work:

JavaScript Includes

Example 1

In the upcoming example, we will be searching for the term โ€˜JavaScriptโ€™ from the given string using the includes() method:


let str = 'Letโ€™s learn JavaScript';
console.log(str.includes('JavaScript'));Code language: JavaScript (javascript)


TrueCode language: Bash (bash)

The console sends a boolean True value since the search term was found on the string.

Example 2

Similarly, we will be looking into another example to search for a term in the given string.


let str = 'JavaScript is a lot easier to learn than you think.';
console.log(str.includes('java'));Code language: JavaScript (javascript)


FalseCode language: Bash (bash)

The term โ€˜javaโ€™ is present in the string, yet the console sends a false. Why?

This is because the includes() method is case-sensitive. In the given sentence, the word โ€˜Javaโ€™ starts with an uppercase โ€˜J,โ€™ but our search term begins with a lowercase โ€˜j,โ€™ hence the false value.

Example 3

Here is another example to search for a term in the given string using the second parameter.


let str = 'JavaScript is lot more easier to learn than you think.';
console.log(str.includes('c', 3));Code language: JavaScript (javascript)


TrueCode language: Bash (bash)

By defining the second parameter, we instruct the console to start its search from the nth position. Since we have mentioned โ€˜3โ€™, the search for the given term starts from 3. C is present after the 3rd position, so the output is true.

Example 4

Another example using 2nd parameter:


let str = 'Letโ€™s learn JavaScript.';
console.log(str.includes('c',25));Code language: JavaScript (javascript)


FalseCode language: Bash (bash)

The output is automatically False since the parameter value exceeds the actual length of the given string.

Example 5

Letโ€™s take a look at what happens when you specify a negative value as a 2nd parameter.


let str = 'Letโ€™s learn JavaScript.';
console.log(str.includes('c',-3));Code language: JavaScript (javascript)


TrueCode language: Bash (bash)

The starting index of the string is 0. If a negative value is provided, there will be no significant change, and the entire array will be searched.

Wrapping Up

We believe you now have a better knowledge of how to use the JavaScript includes() method. With the knowledge you have gained today, try it out in your codes to level up your skill.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap