Home Java Javascript-array JavaScript Array Count
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

JavaScript Array Count

Advertisement
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

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

Ask Questions?    Discuss: JavaScript Array Count  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
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; }
DMCA.com