How to solve contains is not a function in JavaScript

How to solve contains is not a function in JavaScript

In this post we will give you information about How to solve contains is not a function in JavaScript. Hear we will give you detail about How to solve contains is not a function in JavaScriptAnd how to use it also give you demo for it if it is necessary.

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 function

In 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); // true

For arrays:

const arr = [2, 3, 4];const result = arr.includes(3);console.log(result); // true

Using 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)); // true

Conclusion

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.

Hope this code and post will helped you for implement How to solve contains is not a function in JavaScript. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US