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 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
.
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:
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.