checking to see this if array is uninitialized

C

chinu

HI all,
i am declaring an array in javascript
var a = new array();

now before assigning a value to the ith element of this array, i have
to check if some value has already been assigned there. But at the
time of defining array, it gives undefined values to the array. Is
there a way/function, thru which i can find, whether a particular
index value was assigned before or not.

regards
chinmay
 
P

pcx99

chinu said:
HI all,
i am declaring an array in javascript
var a = new array();

now before assigning a value to the ith element of this array, i have
to check if some value has already been assigned there. But at the
time of defining array, it gives undefined values to the array. Is
there a way/function, thru which i can find, whether a particular
index value was assigned before or not.

regards
chinmay

ironically the way is to use the "undefined" keyword which javascript
uses to indicate that a variable doesn't technically exist yet.

if (a[0]==undefined) {
array element is undefined
} else {
array element has a value
}
 
R

RobG

HI all,
i am declaring an array in javascript
var a = new array();

now before assigning a value to the ith element of this array, i have
to check if some value has already been assigned there. But at the
time of defining array, it gives undefined values to the array.

Not exactly. When you first declare an array, it is empty - it has no
elements and length zero. You can use an empty initialiser (most
think it preferable to using new Array()):

var a = [];

Is
there a way/function, thru which i can find, whether a particular
index value was assigned before or not.

Re-declaring a variable with var doesn't hurt, so you can do:

var a = a || [];
if (typeof a == 'undefined') {
/* assign value to a */
} else {
/* a has a value, deal with it */
}
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
ironically the way is to use the "undefined" keyword which javascript
uses to indicate that a variable doesn't technically exist yet.


No.

If a variable exists, it must have a value. The default value is of the
undefined type, which has one value which is undefined - not the same as
the string "undefined" of course.

If a variable does not exist, its contents don't exist and cannot be
tested. One can test to see whether a variable exists, using !!,
provided (AFAIR) that it cannot have the values +0, -0, false, "", null,
or undefined. One can I think do better with window.onerror. One may
be able to do better with try...except.

Then there's the "strict equality" operator ...

Once a variable has been created and assigned an "ordinary" value, such
as 3 or "Fred" or tomorrow-lunchtime, it can have the value undefined
assigned to it.

Calling it undefined was very silly, since it is then difficult to talk
about it. It should have been called bogus, which has the merit of
being shorter than a standard tab. Or unecht.

It's a good idea to read the newsgroup and its FAQ. See below.
 
R

Richard Cornford

Dr said:
No.

If a variable exists, it must have a value. ...
<snip>

In addition, - undefined - is not keyword, it is a property of the global
object with the DontEnum and DontDelete attributes, so most reminiscent
of a global variable (global variables don't have the DontEnum
attribute). And as it is not a ReadOnly property it is even possible for
its value to be other than the language's undefined value (though
assigning a different value to the - undefined - global variable would be
a fairly insane thing to do).

Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top