473,569 Members | 2,698 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 11665
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...@g mail.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.javas cript message <93************ *****@newssvr11 .news.prod
igy.net>, Fri, 9 Feb 2007 03:40:53, pcx99 <x@x.composte d:

>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.c om/faq/index.html>.
<URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demo n.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
20269
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
5068
by: Mikhail Teterin | last post by:
Hello! Consider the following simple accessor function: typedef struct { int i; char name; } MY_TYPE; const char *
7
5626
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 strings of any lengths into an array of pointers to char type variable. my problem is: given the declaration
15
4855
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 the program I wish to return this array to its startup state, can this be accomplished by writing binary zeroes to the entire memory block with...
6
3660
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 the example shows a System.IndexOutOfRangeException being thrown. However, in the very next section (Jagged arrays) they have warning section...
3
1528
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 grid. When I use: mBoard = New CBoardPosition (7,7)
15
3823
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> typedef struct _tnode_t { void *content; struct _tnode_t *kids;
21
22895
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
4748
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. --------------------------------- PROGRAMME -------------------------------- /* K&R2 section 1.9 exercise 1.19
0
7612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7672
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7968
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6283
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5512
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1212
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.