var term = [,];
term[0,0]="0";
term[0,1]="1";
term[1,0]="2";
term[1,1]="3";
alert(term[0,1]);
Returns 3, and I don't know why. Logically, it should return 1, correct?
var term = [,];
term[0,0]="0";
term[0,1]="1";
term[1,0]="2";
term[1,1]="3";
alert(term[0,1]);
Returns 3, and I don't know why. Logically, it should return 1, correct?
In JavaScript, when you have an expression like a, b, it will evaluate both a and b and the result of the expression will be b. You can confirm that like this
console.log((1, 2));
# 2
console.log((0, 1));
# 1
console.log((1, 0));
# 0
So,
term[0, 0] = "0";
term[0, 1] = "1";
term[1, 0] = "2";
term[1, 1] = "3";
was evaluated like this
term[0, 0] = term[0] = "0";
term[0, 1] = term[1] = "1";
term[1, 0] = term[0] = "2";
term[1, 1] = term[1] = "3";
So the actual array has got only 2 and 3. Now, you are trying to access, 0, 1, which is equivalent to
term[0, 1] = term[1]
That is why you are getting 3.
To actually create a 2-D Array, you create a main array and keep adding subarrays to it, like this
var term = [];
term.push([0, 1]);
term.push([2, 3]);
Now, to access value at 0, 1, you need to do like this
term[0][1]
By 2D it's meant [[]], not [,] jsBin example
var term = [[],[]];
term[0][0]="0";
term[0][2]="1";
term[1][0]="2";
term[1][3]="3";
console.log( term ); // [["0", "1"], ["2", "3"]]
console.log( term[0][1] ); // "1"
Also you can insert/append keys into an array using Array.prototype.push()
JavaScript arrays don't work that way.
Here is an example that should work:
var term = []; // Create array
term[0] = ["0", "1"]; // What you referred to as [0,0] and [0,1]
term[1] = ["2", "3"]; // What you referred to as [1,0] and [1,1]
alert(term[0][1]); // Proper JavaScript way to access 2D array.
Here is the jsfiddle.