From: Tim Streater on
Let's say I have an array, say:

var myarray = new Array ();

Some time later, I may need to know about myarray(27) as follows:

1) whether it's never yet been created
2) I previously used it but then set it to null

if (1) or (2) is true then I want to take some action.

Can I test that in the following way:

if (typeof(myarray[27])=="undefined" || myarray[27]==null)
{
doactions ...
}

Googling appears to indicate that if I happen to have created
myarray[30], then the "if" statement above would work fine, but I
haven't found an indication of the situation if the highest index in
myarray that I had so far created were, say, 5.

Thanks,

--
Tim

"That the freedom of speech and debates or proceedings in Parliament
ought not to be impeached or questioned in any court or place out of
Parliament"

Bill of Rights 1689
From: David Mark on
Tim Streater wrote:
> Let's say I have an array, say:
>
> var myarray = new Array ();
>
> Some time later, I may need to know about myarray(27) as follows:
>
> 1) whether it's never yet been created
> 2) I previously used it but then set it to null
>
> if (1) or (2) is true then I want to take some action.
>
> Can I test that in the following way:
>
> if (typeof(myarray[27])=="undefined" || myarray[27]==null)

As the latter comparison is not strict, it renders the first superfluous.

> {
> doactions ...
> }
>
> Googling appears to indicate that if I happen to have created
> myarray[30], then the "if" statement above would work fine, but I
> haven't found an indication of the situation if the highest index in
> myarray that I had so far created were, say, 5.
>

Well, you could try it and see. ;)
From: Thomas 'PointedEars' Lahn on
Tim Streater wrote:

> var myarray = new Array ();

Since this is a constructor *call*, I would omit the space before the
argument list. But

var myarray = [];

suffices these days.

> Some time later, I may need to know about myarray(27) as follows:

Probably you mean myarray[27].

> 1) whether it's never yet been created
> 2) I previously used it but then set it to null
>
> if (1) or (2) is true then I want to take some action.
>
> Can I test that in the following way:
>
> if (typeof(myarray[27])=="undefined" || myarray[27]==null)

`typeof' is an operator, not a function, so you should either omit the
parentheses or place a space before them.

> {
> doactions ...
> }
>
> Googling appears to indicate that if I happen to have created
> myarray[30], then the "if" statement above would work fine, but I
> haven't found an indication of the situation if the highest index in
> myarray that I had so far created were, say, 5.

var i = 5;
if (myarray.length < (i + 1) || myarray[i] === null)
{
// ...
}


PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
From: Jorge on
On Mar 10, 12:23 pm, Tim Streater <timstrea...(a)waitrose.com> wrote:
> Let's say I have an array, say:
>
> var  myarray = new Array ();
>
> Some time later, I may need to know about myarray(27) as follows:
>
> 1) whether it's never yet been created
> 2) I previously used it but then set it to null
>
> if (1) or (2) is true then I want to take some action.
>
> Can I test that in the following way:
>
> if  (typeof(myarray[27])=="undefined" || myarray[27]==null)
>       {
>       doactions ...
>       }
>
> Googling appears to indicate that if I happen to have created
> myarray[30], then the "if" statement above would work fine, but I
> haven't found an indication of the situation if the highest index in
> myarray that I had so far created were, say, 5.
>

var el= myarray[n];
if (n >= myarray.length) {

//myarray[n] does not exist
//e.g. [1,2,,3][27]

} else if (!(n in myarray)) {

//myarray[n] is a hole
//e.g. [1,2,,3][2]

} else if ((el === undefined) || (el === null)) {

//exists, but its value is undefined or null
//e.g. [1,2,undefined,3][2] or [1,2,null,3][2]
}
--
Jorge.
From: Tim Streater on
On 10/03/2010 11:32, Thomas 'PointedEars' Lahn wrote:
> Tim Streater wrote:
>
>> var myarray = new Array ();
>
> Since this is a constructor *call*, I would omit the space before the
> argument list. But
>
> var myarray = [];
>
> suffices these days.
>
>> Some time later, I may need to know about myarray(27) as follows:
>
> Probably you mean myarray[27].

Typo.

>> 1) whether it's never yet been created
>> 2) I previously used it but then set it to null
>>
>> if (1) or (2) is true then I want to take some action.
>>
>> Can I test that in the following way:
>>
>> if (typeof(myarray[27])=="undefined" || myarray[27]==null)
>
> `typeof' is an operator, not a function, so you should either omit the
> parentheses or place a space before them.

Yep.

>> {
>> doactions ...
>> }
>>
>> Googling appears to indicate that if I happen to have created
>> myarray[30], then the "if" statement above would work fine, but I
>> haven't found an indication of the situation if the highest index in
>> myarray that I had so far created were, say, 5.
>
> var i = 5;
> if (myarray.length< (i + 1) || myarray[i] === null)
> {
> // ...
> }

Thanks, I'll try that.

--
Tim

"That the freedom of speech and debates or proceedings in Parliament
ought not to be impeached or questioned in any court or place out of
Parliament"

Bill of Rights 1689