JavaScript string.includes() Method
-
Syntax of
string.includes()
: -
Example Codes: Use
string.includes()
Method to Find a String with Required ParametertargetString
-
Example Codes: Use
string.includes()
Method to Find a String with Optional ParameterindexPosition
-
Example Codes: Use
string.includes()
Method to Find a String with Case Sensitivity -
Example Codes: Use
string.includes()
Method to Find a String in Real-World Scenario
In Programming languages, Strings are considered a series of characters. JavaScript string is not out from that, but the interesting thing JavaScript does is automatically convert strings to a String Object. As a result, we can easily access the methods and properties of a String with a (.) notation. The string.includes()
method is one of them.
Syntax of string.includes()
:
string.includes(targetString, indexPosition)
Parameters
targetString |
A specific string that you want to search for. It is Required. |
indexPosition |
A specific index position from where you want to start searching. By default, it starts from the index position 0. It is optional |
Return
A Boolean value from the string
Returns true
if the string exists; otherwise, false
.
Important: the string.includes()
Method is case-sensitive. That means it will not match if the actual string is in upperCase
form and you search for lowerCase
from.
Example Codes: Use string.includes()
Method to Find a String with Required Parameter targetString
let str = 'Good things come to those who wait'
console.log(str.includes('come'))
console.log(str.includes('go'))
Output:
true
false
We provide a string come
to check whether it exists in the str
or not. In that case, the str.includes()
method finds the string and returns true
We provide another sting go
which does not exist in the str
and returns false
Example Codes: Use string.includes()
Method to Find a String with Optional Parameter indexPosition
let str = 'Look before you leap'
console.log(str.includes('before', 5))
console.log(str.includes('before', 10))
Output:
true
false
We provide a string before
with an index position 5
. The str.includes()
method to start searching the targeted string from the index position 5. The given string before
starts from the index position 5 and returns true
.
We also check the same string, but we provide the index position 10
this time. As a result, the method cannot find the string and returns false
.
Example Codes: Use string.includes()
Method to Find a String with Case Sensitivity
let str = 'FACE THE MUSIC'
console.log(str.includes('FACE'))
console.log(str.includes('face'))
Output:
true
false
We provide a string FACE
in the upper case form and the str.includes()
method finds the string at the exact form and returns true
.
We provide the same string face
but this time in the lower case form. Even the letters are the same, but it does not match the form with the actual string, and the str.includes()
method returns false
.
Example Codes: Use string.includes()
Method to Find a String in Real-World Scenario
let fruitStore = ['Apple', 'Orange', 'Mango', 'Cherry', 'Graps']
console.log(fruitStore.includes('Orange'))
console.log(fruitStore.includes('Banana'))
Output:
true
false
Suppose we have an e-commerce application where we sell different fruits. We have an array of strings on our program that stores all the fruit’s names. Now, we want to check if a specific fruit is available in the store or not. To do so, we have used the str.includes()
method.
We provide a string Orange
to check whether it exists on the fruitStroe
or not. The includes()
method finds the fruit and returns true
.
We provide another string, Banana
, to the includes()
method. This time it does not find any fruit names Banana
and returns false
.
Niaz is a professional full-stack developer as well as a thinker, problem-solver, and writer. He loves to share his experience with his writings.
LinkedIn