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

Array indexing problem

P: n/a
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
Share this Question
Share on Google+
2 Replies


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

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