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 Repl.it for you guys so you can play around with the code below.

πŸ”₯ Learn JavaScript Programming - Beginner Friendly!
πŸ”¨ JavaScript Basics
πŸ‘‰Β JavaScript Array Filter
πŸ‘‰Β JavaScript Array Find
πŸ‘‰ JavaScript forEach (Arrays)
πŸ‘‰Β JavaScript Slice (Arrays)
πŸ‘‰Β JavaScript Spread Operator
πŸ‘‰Β JavaScript Slice(Strings)
πŸ‘‰Β JavaScript Includes
πŸ‘‰ JavaScript alert()
πŸ‘‰Β Store User Input in a variable with JS
πŸ‘‰Β Difference between Let and Const
πŸ‘‰Β Sorting Numbers in JS
βš™οΈ JavaScript Specifics
πŸ‘‰ 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

Syntax:

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:

Code

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

Output

True
Code 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.

Code

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

Output

False
Code 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.

Code

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

Output

True
Code 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:

Code

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

Output

False
Code 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.

Code

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

Output

True
Code 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