Briefly
This method is defined for arrays and strings.
For arrays: checks if the sought element exists in the array.
For strings: checks if the sought substring exists in the string.
Returns true
if the sought element was found, and false
— if not 😎
Example
The method takes two arguments — the value to check, and the position from which to start checking. The second argument is optional and defaults to 0
.
Features of using the second argument
If a positive value or 0
is passed as the argument, the search will start from this index to the end of the array.
If a negative value is passed, the search will start from this index, counted from the end of the array, and will proceed to the end of the array. For this case, the starting index of the search can be calculated using the formula length of array
. For example, if the length of the array/string is 10
, and the passed argument is -2
. The search will start at position 8
, since 10 +
.
If the second argument is greater than the length of the array/string, the method will always return false
.
Array:
const dead = ['Jon Snow', 'Joffrey', 'Ned Stark', 'Night King']const isAryaDead = dead.includes('Arya Stark')console.log(isAryaDead)// falseconst isJoffreyDead = dead.includes('Joffrey')console.log(isJoffreyDead)// trueconst isJohnDead = dead.includes('Jon Snow', 1)console.log(isJohnDead)// false
const dead = ['Jon Snow', 'Joffrey', 'Ned Stark', 'Night King'] const isAryaDead = dead.includes('Arya Stark') console.log(isAryaDead) // false const isJoffreyDead = dead.includes('Joffrey') console.log(isJoffreyDead) // true const isJohnDead = dead.includes('Jon Snow', 1) console.log(isJohnDead) // false
String:
const text = 'Look, it’s nearby our panda. We are running with you as if from a cheetah.'console.log(text.includes('panda'))// trueconsole.log(text.includes('Monkey'))// false// The search is case-sensitiveconsole.log(text.includes('Panda'))// false
const text = 'Look, it’s nearby our panda. We are running with you as if from a cheetah.' console.log(text.includes('panda')) // true console.log(text.includes('Monkey')) // false // The search is case-sensitive console.log(text.includes('Panda')) // false
In practice
Advice 1
🛠 Use the method when you need to ensure that the object is in the array. For example, to avoid adding the same value twice.
🛠 Be careful when passing objects to includes
. If two objects look identical, it doesn't necessarily mean they are the same object because objects are stored by reference.
const phoneContacts = [ { name: 'Ivan', lastName: 'Taranov' }, { name: 'Igor', lastName: 'Ivanov' }, { name: 'Mom', lastName: '' },]console.log(phoneContacts.includes( { name: 'Mom', lastName: '' }))// false
const phoneContacts = [ { name: 'Ivan', lastName: 'Taranov' }, { name: 'Igor', lastName: 'Ivanov' }, { name: 'Mom', lastName: '' }, ] console.log(phoneContacts.includes( { name: 'Mom', lastName: '' } )) // false
Here console
will output false
, as we created a new object, even though it looks the same as the one in the array.