In JavaScript, .contains()
and .includes()
are used for different purposes:
.contains()
: This is a method of the Node interface in the Document Object Model (DOM). It’s used to check whether a node is a descendant of a given node, i.e., child, grandchild, etc.
node.contains(otherNode)
.includes()
: This is a method of the Array and String prototypes in JavaScript. It’s used to determine whether an array includes a certain value among its entries or whether a string includes a certain substring.
For arrays:
array.includes(valueToFind, fromIndex)
For strings:
string.includes(searchString, position)
So, the main difference is that .contains()
is used with DOM nodes, while .includes()
is used with arrays and strings.