I am trying to get through some Javascript problems when this one caught me off guard. Have a look at the following snippet,
var person = []; person['1'] = "John"; person['2'] = "Doe"; person['3'] = 46; //[1: "John", 2: "Doe", 3: 46] console.log(person); //4 <-- as i expected console.log(person.length); var person2 = []; person2['a'] = "John"; person2['b'] = "Doe"; person2['c'] = 46; //[1: "John", 2: "Doe", 3: 46] console.log(person2); //0 <-- i expected 4, but got 0 console.log(person2.length) I have added the output in comments.
I am trying to figure out why
console.log(person2.length) gives length 0 instead of 4. Can someone please help me understand this?
person2as an object, not an array. Objects don't have a length.