Implementing an Array.count() Method in JavaScript
As much as I write about MooTools on my blog, I spend most of my work day knee-deep in PHP. As you probably know, one way to get the size of an array in PHP is to use the count() function:
echo count($my_array);
In JavaScript, the way to get the size of an array is to use the length property, like this:
alert(my_array.length);
For some reason, I absolutely hate the ".length" way of retrieving the length of an array. For this reason, I've implement the count() method into JavaScript:
Array.prototype.count = function() {
return this.length;
};
The count() habit is difficult to break, so why try?
![An Interview with Eric Meyer]()
Your early CSS books were instrumental in pushing my love for front end technologies. What was it about CSS that you fell in love with and drove you to write about it?
At first blush, it was the simplicity of it as compared to the table-and-spacer...
![Create a Sheen Logo Effect with CSS]()
I was inspired when I first saw Addy Osmani's original ShineTime blog post. The hover sheen effect is simple but awesome. When I started my blog redesign, I really wanted to use a sheen effect with my logo. Using two HTML elements and...
![JavaScript Copy to Clipboard]()
"Copy to clipboard" functionality is something we all use dozens of times daily but the client side API around it has always been lacking; some older APIs and browser implementations required a scary "are you sure?"-style dialog before the content would be copied to clipboard -- not great for...
![MooTools Accordion: Mouseover Style]()
Everyone loves the MooTools Accordion plugin but I get a lot of requests from readers asking me how to make each accordion item open when the user hovers over the item instead of making the user click. You have two options: hack the original plugin...
Thanks for your blog. you offer great advice and tutorials and for some odd reason, if I am looking to see how to complete a task, you have a tutorial on it or at least an idea on the concept I am trying to achieve; so thanks David. Although this post came about 2 days too late. :)
Do you do this for strings as well?
@Binny: No, I don’t. Just arrays.
This is all fine and well, but length is fundamentally different from count.
Take the following example:
var myArray = [‘one’, ‘two’, ‘three’];
// myArray.length == 3, as we would expect
var myOtherArray[100] = ‘one’;
//myArray.length == 100, NOT 1
Even though we only have 1 item in the array, ‘length’ refers to the largest index + 1. Unlike PHP where we can use numeric keys and strings for indexes in an ad hoc way and still get an accurate count via count(), in JS it’s not quite the same.
In a way, this is just splitting hairs, but a lot of people already don’t understand JS’s length property, and referring to it as “count” only serves to further the confusion.
Correction to my previous post:
the second code snippet would be:
var myOtherArray = [];
myOtherArray[100] = ‘one’;
and its length property would actually return 101.
That’s right and that’s what I was expecting when searching for count on js. This should do it:
Riv, thats a much better form of the count use. Its surprising that this is not built into javascript.
I think it’s okay for personal project but it has a counter side when you are in enterprise. The maintainability is harder when people use custom extension (prototype existing framework object). It cane be confusing. In some case, I have seen people creating function to simply get an object inside an array. I think this should be used with parsimony. But, it’s just my opinion…
This is a working one. It counts function elements too.
I’ve just bumped into learning Js, I try to be as attentive as possible but, I don’t find a way. So, may any one help me how to understand Js programming.
OR
Wow, no functionality to edit comments? What is this, the year 2001?
Querying the length is faster than invoking a method, but the habit of using
.count()
is interesting.In python is used
len([1, 2, 3])
function to get the count, so the JavaScript’s length is not so bad :).