473,503 Members | 10,046 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

checking to see this if array is uninitialized

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
4 11656
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
20262
by: rswanster | last post by:
When I compile and run the following: #include <iostream> int main() { bool f; std::cout << f << std::endl; f = not(f); std::cout << f << std::endl; }
99
5040
by: Mikhail Teterin | last post by:
Hello! Consider the following simple accessor function: typedef struct { int i; char name; } MY_TYPE; const char *
7
5620
by: arkobose | last post by:
hey everyone! i have this little problem. consider the following declaration: char *array = {"wilson", "string of any size", "etc", "input"}; this is a common data structure used to store...
15
4837
by: Charles Sullivan | last post by:
Assume I have a static array of structures the elements of which could be any conceivable mixture of C types, pointers, arrays. And this array is uninitialized at program startup. If later in...
6
3654
by: Flip | last post by:
I'm reading the O'Reilly's Progamming C# book and I have a question about array bounds checking. On page 174, near the top, they show an example where c# does indeed to array bounds checking cause...
3
1524
by: HateSpam | last post by:
I am defining a class that has, as a member, an array of another user-defined class. private mBoard as CBoardPosition() The problem comes when I attempt to size the array, it should be an 8x8...
15
3814
by: Paminu | last post by:
Still having a few problems with malloc and pointers. I have made a struct. Now I would like to make a pointer an array with 4 pointers to this struct. #include <stdlib.h> #include <stdio.h>...
21
22888
by: vito | last post by:
how to achieve that? it seems php doesn't support it well for a C programmer? i hope to use something like: a; a; a;
8
4741
by: arnuld | last post by:
i have created a solutions myself. it compiles without any trouble and runs but it prints some strange characters. i am not able to find where is the trouble. ...
0
7095
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7294
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7361
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4693
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3183
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1523
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
749
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
403
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.