0

Is there a good built-in way that I can find whether value is a array or not?

One simple check I can think of is as follows, but I don't like it:

if(ele.push){ /* its array it has push method */ }

I mean i would like know if something like pseudo-code below exists. typeof doesn't seems to be applicable, as it only returns "object" (though that makes sense).

function x(ele){ if(isArray(ele)){ /* dosomething() */ } }
1

3 Answers 3

2

http://www.andrewpeace.com/javascript-is-array.html

<script type="text/javascript">
  function is_array(input){
    return typeof(input)=='object'&&(input instanceof Array);
  }
</script>
Sign up to request clarification or add additional context in comments.

Comments

0
element.constructor == Array

Comments

0

Not the cleanest but...

function isArray(obj) {
    return obj.constructor == Array;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.