In JavaScript, the includes()
method can work both with Arrays
and Strings
. Because of this peculiarity, reviewing both use cases in detail is essential to shedding more light on this method. The crucial similarity between using the includes()
method for each data set is that it returns true
or false
.
Array.includes() method
This method can be used to determine whether an array includes a particular value among other entries. As this method always works, the output you get will always include a true
or false
value as appropriate. Syntax-wise, this method is written like this when working with Arrays
:
array.includes(element, start)
While the first part is clear, the parameters should be elaborated on independently. The element
stands for the value you're looking for, while the start
indicates the starting position. Please note that the latter parameter is optional, and you can use the includes()
method omitting it.
Array use cases
Because this method is solely used to determine whether an array includes or excludes a specified value, its use cases are somewhat limited. Still, the most common example you have to learn is to work with numbers in arrays. Consider the following example:
console.log([5,6,7].includes(5)); // Output: true
console.log([5,6,7].includes(9)); // Output: false
console.log([5,6,7].includes(6,6)); // Output: false
The example we included is easy and readable, so you can guess what output you get with each query. Another example how this method works will show how you can get the false
value when the start
value is greater or equal to the array's length:
let arr = ['z', 'x', 'c']
console.log(arr.includes('x', 150)); // Output: false
console.log(arr.includes('x', 300)); // Output: false
In this case, the start
value is not corresponding to the array's content, always returning the false
output, regardless of how you manipulate the data afterward.
String.includes() method
In terms of working with a String
, essentially, this method always performs a case-sensitive search to determine if one string can be found within another string, returning true
and false
values, respectively. Take a look at how this method is expressed in code syntax:
includes(searchString, position)
Two parameters speak for themselves. A searchString
is a parameter that indicates a string to be searched within another str
. Position
is optional, which can specify where to start searching for a string, with a default value of 0
. The return value, in turn, works similarly to an Array
, returning true when the string can be found and false
if not. Review the following example:
const sentence = 'We are learning JavaScript together!';
const word = 'JavaScript';
console.log(`The word "${word}" ${sentence.includes(word) ? 'is' : 'is not'} in the sentence`);
// Output: The word "JavaScript" is in the sentence
In this case, we specified a whole sentence, looking for a word within it. Using the parameters we've entered, the includes()
method found that the word 'JavaScript
' is located within the string. And what about the false
return? Take a look:
let text = "And how about this sentence?";
console.log(text.includes("this", 777)); //Output: false
As you can see, the word this
is located within a sentence, but the second value 777
is not. That's why the output you get is false
.
String use cases
Even though we highlighted the case sensitivity, it's best to illustrate a few examples. And because the includes()
method has limited functionality, there aren't many use cases, but still take a look at the following:
const str = 'The question is: How do we learn JavaScript fast?'
console.log(str.includes('how')) // Output: false
console.log(str.includes('How')) // Output: true
console.log(str.includes('JavaScript')) // Output: true
console.log(str.includes('JAVASCRIPT', 1)) // Output: false
console.log(str.includes('javascript')) // Output: false
console.log(str.includes('')) // Output: true
In this case, we specified a sentence string, followed by using the includes()
method for determining whether particular words are present in it. Since this approach is case-sensitive, 'how' is returned as false
, while 'How' is shown as true
.
The same happens with the three examples writing JavaScript, with the first one being correct and the others invalid. The last console log is a query specifying if the string is present, which returns a true
value.
Test yourself
While the includes()
method is not complicated, learning how to work with both Array
and String
data types is necessary. Consider completing the following three exercise types designed by our team:
Task 1. Determine whether an array with numbers
7
,4
,85
,25
,41
,79
,542
,16125
, and2
includes6.9
,41
, and2
.
Solution:
console.log([7, 4, 85, 25, 41, 79, 542, 16125, 2].includes(6.9, 41, 2)); // Output: false
But note, that if you use includes()
separately for each number it will return true
for 41
and 2
and false for 6.9
.
Task 2. Working with an array with numbers 5, 6, 7, find out if that's possible to determine if it includes
5
.
Solution:
console.log([5,6,7].includes(5)); // Output: true
Task 3. Write the constant string with a sentence: 'This is a sample sentencE!' and find if it includes words:
this
,sample
,sentence
using the console log.
Solution:
const str = 'This is a sample sentencE!'
console.log(str.includes('this')) // Output: false
console.log(str.includes('sample')) // Output: true
console.log(str.includes('sentence')) // Output: false