For instance, if I do a[1000000]=1; will it use memory for 1000000 elements or just for this one?
Would 1,000,000 elements be created?
No, arrays are sparse, but their index will be persistent. EDIT: Actually, their sparseness would be implementation-specific, but keeping them sparse in case of a[1000000] = 1
would seem a logical thing to me.
var a = [1, 2, 3, 4];
var x = a[1]; // -> x := 2
delete a[1];
var y = a[1]; // -> y := undefined
a[9] = 10;
var y = a[8]; // -> z := undefined
Are JS arrays associative?
JavaScript arrays are a subset of associative arrays (in that indices have to be integers, as shown in KennyTM's answer. JavaScript objects are fully associative:
var o = { "key1": "value1", "key2": "value2" };
var i = "key2";
var v = o[i]; // -> v := "value2"
JS arrays are auto-growing. Setting a[100] to 1 on an empty array will populate the first 99 elements with "undefined".
You may use object literal as a kind of 'associative aray' in some cases:
var object = {
"first": "1",
"second": "2",
"third": "3",
"fourth": "4"
};
object.fifth = "5";
object.["sixth"] = "6";
But it has its limitations... There is no magic 'length' parameter and you will not have access to methods that every array has.