Home Java Javascript-array JavaScript Array Contains Method

Related Tutorials


 
 

Share on Google+Share on Google+

JavaScript Array Contains Method

Advertisement
In the given JavaScript array example, we are going to show how an array contains method().

JavaScript Array Contains Method

     

In the given JavaScript array example, we are going to show how an array contains method(). As you already know we required the property 'prototype' of Array class whenever we want to add or modify the methods and properties of an array. That means if we are using method contain(), we have to use prototype property in our example. Now let's see how we have used it..

JavaScript code to use contain method in an array

<html>
<head>
<h2>Use of contains() method</h2>
<script>
Array.prototype.contains = function (element) {
for (var i = 0; i < this.length; i++) {
if (this[i] == element) {
return true;
}
}
return false;
}
arr1 = ["Rose", "India", "Technologies"];
document.write("The condition is "+arr1.contains("India")+"<br>");
</script>
</head>
<b>[If the specified element is present in the array, it returns true otherwise 
returns false.]</b>
</html>

 In the given code the contain method determines whether the specified element is present in the array or not. If the specified element is present in the array, it returns true otherwise it returns false.

Output of the code will be displayed as:

Download Source Code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 11, 2008

Related Tutorials

Discuss: JavaScript Array Contains Method  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com