By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,641 Members | 1,098 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,641 IT Pros & Developers. It's quick & easy.

checking to see this if array is uninitialized

P: n/a
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

Feb 9 '07 #1
Share this Question
Share on Google+
4 Replies

P: n/a
chinu wrote:
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
}

--
http://www.hunlock.com -- Musings in Javascript, CSS.
$FA
Feb 9 '07 #2

P: n/a
On Feb 9, 1:21 pm, "chinu" <chinu.pan...@gmail.comwrote:
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[i] == 'undefined') {
/* assign value to a[i] */
} else {
/* a[i] has a value, deal with it */
}
--
Rob

Feb 9 '07 #3

P: n/a
In comp.lang.javascript message <93*****************@newssvr11.news.prod
igy.net>, Fri, 9 Feb 2007 03:40:53, pcx99 <x@x.composted:

>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.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Feb 10 '07 #4

P: n/a
Dr J R Stockton wrote:
Fri, 9 Feb 2007 03:40:53, pcx99 wrote:
>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. ...
<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.

Feb 10 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.