6

Say I have a function like so:

function foo(bar) {
    if (bar > 1) {
       return [1,2,3];
    } else {
       return 1;
    }
}

And say I call foo(1), how do I know it returns an array or not?

1
  • 3
    You're making your life harder than it needs to be. I would consider it better style if the function always returns an array. Commented Jul 30, 2009 at 19:01

5 Answers 5

16

I use this function:

function isArray(obj) {
  return Object.prototype.toString.call(obj) === '[object Array]';
}

Is the way that jQuery.isArray is implemented.

Check this article:

Sign up to request clarification or add additional context in comments.

2 Comments

Perfect! I am using jQuery so I'll just use that. The article helped too.
in modern browsers we have Array.isArray function, so better write smth like if !Array.isArray { ... here goes the definition ...}
3
if(foo(1) instanceof Array)
    // You have an Array
else
    // You don't

Update: I have to respond to the comments made below, because people are still claiming that this won't work without trying it for themselves...

For some other objects this technique does not work (e.g. "" instanceof String == false), but this works for Array. I tested it in IE6, IE8, FF, Chrome and Safari. Try it and see for yourself before commenting below.

12 Comments

That will only work, if you declare an array via new Array(), not with the [] shorthand. See the section 'Description' under the above URL.
And this won't work for different global contexts; like an object from within an iframe...
@Boldewyn, that is not true. It is true for some other objects (e.g. "" instanceof String == false), but this works for Array. Try it and see for yourself.
@Josh Stodola, so can Object.prototype.toString. You can overwrite nearly everything in js, but there are some assumptions you have to make in order to answer a question.
|
3

As of ES5 there is isArray.

Array.isArray([])  // true

Comments

2

Here is one very reliable way, take from Javascript: the good parts, published by O'Reilly:

if (my_value && typeof my_value === 'object' &&  typeof my_value.length === 'number' &&
!(my_value.propertyIsEnumerable('length')) { // my_value is truly an array! }

I would suggest wrapping it in your own function:

function isarray(my_value) {

    if (my_value && typeof my_value === 'object' &&  typeof my_value.length === 'number' &&
        !(my_value.propertyIsEnumerable('length')) 
         { return true; }
    else { return false; }
}

1 Comment

You could just return the result of the expression instead of an if..else. :P
0

To make your solution more general, you may not care whether it is actually an Array object. For example, document.getElementsByName() returns an object that "acts like" an array. "Array compliance" can be assumed if the object has a "length" property.

function is_array_compliant(obj){
    return obj && typeof obj.length != 'undefined';
}

2 Comments

Strings have a "length" property, too.
That's true. So, you should also check that: typeof obj == "object"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.