JavaScript Array Count

In the given JavaScript example we are going to show you, how to count the length of array.

JavaScript Array Count

     

In the given JavaScript example we are going to show you, how to count the length of array. For that we have to use the count() method in our code and that can be used only if we are using the property 'prototype' of Array class in our example. You can see all these declaration in the given syntax..

Here is the JavaScript code for getting the length of an array.

<html>
<head>
<h2> Count() Method in Javascript</h2>
<script>
Array.prototype.count = function () {
return this.length;
}
arr = [ 2, 4, 6, 8, 10];
arr1=arr.count();
document.write("<b>The length of Array is </b>"+arr1);
</script>
</head>
<body bgcolor="lightBlue"/>
</html>

Output will be displayed as:

In the given output, 5 is the length of array.

Download Source Code

Advertisements

 

We have 1000s of tutorials on our website. Search Tutorials tutorials on our website.

Share on Google+Share on Google+

JavaScript Array Count

Posted on: November 11, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: JavaScript Array Count  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:2
Clive Walkden
June 16, 2011
Update

Isn't creating a function a waste of code? Surely this would be easier. arr = [ 2, 4, 6, 8, 10]; arr1=arr.length; document.write("<b>The length of Array is </b>"+arr1);
Chirtsian
September 27, 2011
Works with associative arrays

Array.prototype.count = function () { for (k in this) { if (this[k].constructor !== Function) { this.length++; } }; return this.length; }

Ads

 

Ads