mrblah
mrblah

Reputation: 103727

if array exists, then continue

I have an array that might be present in my .aspx page, if it is, I want to fire a javascript function.

I tried:

if(someArray)
       blah(someArray);

but I get an error when I havent' defined someArray.

Upvotes: 2

Views: 924

Answers (3)

Shadow2531
Shadow2531

Reputation: 12170

var a = [];
var b = new Array();
alert(typeof a === "object" && a instanceof Array);
alert(typeof b === "object" && b instanceof Array);
alert(typeof c === "object" && c instanceof Array);

Upvotes: 0

Soviut
Soviut

Reputation: 91742

You should probably be pre-defining the array as null and check to see if it resolves, rather than sometimes available.

Array someArray = null;

// this is where you'll populate or replace someArray
// if you don't, someArray simply remains empty

if (someArray)
{
    ...
}

Upvotes: 3

Paolo Bergantino
Paolo Bergantino

Reputation: 488744

if(typeof someArray !== 'undefined') {
    blah(someArray);
}

Upvotes: 8

Related Questions