Javascript isArray() : Check if an element/object is an Array

I work on the front-end part of Zero RRD framework again. It was a very basic interface and the usability wasn't, well isn't that good.

The new stuff is mostly JavaScript and I needed to check if a variable is an array or not. I found the snippet at bram.us, but it isn't fully working for me.

The problem is with variables that are null in that case you'll run into an error. The slightly improved version takes that into account.

  1. function isArray(obj) {
  2. if (obj == null) return false;
  3. return obj.constructor == Array;
  4. }

Trackback URL for this post:

http://www.schnuckelig.eu/trackback/141
Your rating: None Average: 5 (1 vote)

Pingback

[...] Javascript isArray() : Check if an element/object is an Array 16 Apr 2009. Free DNS EveryDNS.net and Bind 9 setup. 04/05/2010 - 07:19. Updated Debian Lenny eAccelerator packages again (AMD64). 3 weeks 6 hours ago; Pingback. I work on the front-end part of Zero RRD framework again. Javascript isArray() : Check if an element/object is an Array [...]

Pingback

[...] Javascript isArray() : Check if an element/object is an Array 16 Apr 2009. Free DNS EveryDNS.net and Bind 9 setup. 04/05/2010 - 07:19. Updated Debian Lenny eAccelerator packages again (AMD64). 3 weeks 6 hours ago; Pingback. I work on the front-end part of Zero RRD framework again. Javascript isArray() : Check if an element/object is an Array [...]

...

The constructor property of the Array object can be overwritten.
better:

  1. var isArray = function(obj) {
  2. return toString.apply( obj ) === toString.apply( [] );
  3. }

not entirely sure here but

not entirely sure here but why is....

  1. return obj instanceof Array;

not used?

btw the captcha's are incredibly noisy - difficult to decipher