I have the following code:
var intrebari = new Array();
var i = 0;
intrebari[i]['enunt'] = 'test';
alert(intrebari[i]['enunt']);
The problem is that when I run it it says that intrebari is undefined. Why?
Yes interbari[0] is null , so it cannot be object - and for adding into array use push instead of indexes
var intrebari = [];
intrebari.push({ 'enunt': 'test' });
alert(intrebari[i]['enunt']);
This will work
intrebari[i] could be set to just {}, and then that assignment in the original would work. But this answer is correct.var intrebari = new Array();
var i = 0;
intrebari[i] = new Object()
intrebari[i]['enunt'] = 'test';
alert(intrebari[i]['enunt']);
[]is the same as "new Array()", and{}is the same as "new Object()". Just to save you some typing :-)