How to solve contains is not a function in JavaScript
In this tutorial, we are going to learn about how to solve the TypeError contains is not a function in JavaScript
When we use a contains() method on a value which is not an valid HTML DOM node, we will get the following errors in our console.
Example:
const fruit = "apple";
fruit.contains("a");Output:
"TypeError: fruit.contains is not a functionIn the example above, we are checking if the character "a" is available in a string or not using contains() method, so we are getting the error in our console because the  contains() only available for HTML dom nodes.
To solve the error, use the includes() method to check if a value is available or not in a strings or arrays.
Here is an example:
const fruit = "apple";
const result = fruit.includes("a");
console.log(result); // trueFor arrays:
const arr = [2, 3, 4];
const result = arr.includes(3);
console.log(result); // trueUsing contains method for dom nodes
To check if a dom node is available in a document or not, we can use the contains() method in JavaScript.
Here is an example, that checks if a header dom node is present in the document or not.
const header = document.getElementById("header");
console.log(document.contains(header)); // trueConclusion
The “contains is not a function” error occurs when we call a contains() method on a value other than the dom nodes. To solve the error use includes() method for strings and arrays.


