views:

265

answers:

3

Hi,

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.

+7  A: 
if(typeof someArray !== 'undefined') {
    blah(someArray);
}
Paolo Bergantino
+3  A: 

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)
{
    ...
}
Soviut
does it work where the array has no element? don't think so!
Sepehr Lajevardi
I wouldn't even bother to create an empty array. Null is perfectly fine:var someArray = null;// Initializeif(someArray) alert(someArray);
Sergej Andrejev
Good call, I adjusted my code
Soviut
A: 
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);
Shadow2531