469,306 Members | 1,903 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

Array indexing problem

Consider the following javascript:

var temp = new Array(new Array(0))
document.writeln(temp[0][0])

temp = new Array(new Array(0,1))
document.writeln(temp[0][0])

One would assume that it would print "0 0" that is the first elements
of the arrays, but it prints "undefined 0". Why does temp[0][0] return
undefined when there is only one element in the array but returns the
first element correctly when there are at least two elements?

--
Antti
Jul 20 '05 #1
2 1435
>Consider the following javascript:

var temp = new Array(new Array(0))
document.writeln(temp[0][0])

temp = new Array(new Array(0,1))
document.writeln(temp[0][0])

One would assume that it would print "0 0" that is the first elements
of the arrays, but it prints "undefined 0". Why does temp[0][0] return
undefined when there is only one element in the array but returns the
first element correctly when there are at least two elements?


That is because the behaviour of the Array object is different
when you pass two or more integers to the Array constructor.

If you pass one integer to the Array constructor it will create
an Array of that size. If you pass two or more integers it will
use them as elements in the array.

In your example,

var temp = new Array(new Array(0))

new Array( 0) creates an array of zero length.

temp = new Array(new Array(0,1))

new Array( 0,1) creates an array that contain [ 0, 1].


Peace, Vm
Yaz

Providing complicated solutions to simple problems since 1997.
Jul 20 '05 #2
to heave chunks wrote:
Consider the following javascript:

var temp = new Array(new Array(0))
document.writeln(temp[0][0])

temp = new Array(new Array(0,1))
document.writeln(temp[0][0])

One would assume that it would print "0 0" that is the first elements
of the arrays, but it prints "undefined 0". Why does temp[0][0] return
undefined when there is only one element in the array but returns the
first element correctly when there are at least two elements?


That is because the behaviour of the Array object is different
when you pass two or more integers to the Array constructor.

If you pass one integer to the Array constructor it will create
an Array of that size. If you pass two or more integers it will
use them as elements in the array.


More, how it is interpreted depends on the implementation. To be
sure, use Array literals:

var temp = [[0]];

creates an Array object with an Array object as only element which
only element is zero and stores a reference to it in `temp'. The
"same" can be accomplished with leaving out constructor arguments,
but requiring another variable:

var help = new Array();
help[0] = 0;
var temp = new Array(help);

Note that although temp[0][0] retrieves `0', deleting or overwriting
`help[0]' changes that value since the reference is stored as element,
not the primitive value of zero:

help[0] = 42;
alert(temp.join(",")); // 42
PointedEars

P.S.
Your `From' is borken, read and follow
<http://www.interhack.net/pubs/munging-harmful/>
if you want to be read in the future.
Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Michael Drumheller | last post: by
29 posts views Thread by shmartonak | last post: by
10 posts views Thread by Dennis Myrén | last post: by
26 posts views Thread by jacob navia | last post: by
4 posts views Thread by Grace Fang | last post: by
33 posts views Thread by Zytan | last post: by
3 posts views Thread by Rüdiger Werner | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.