0

var objArray = []; var object = {};

I am defining every index of the array by adding :

objArray[objArray.length] = object;

And I can access the length of the objArray by doing

objArray.length;

However, if I try to do

objArray[objArray.length], 

it gives me undefined.

Why is this?

1 Answers1

1

Because indexing of arrays starts with 0, but when there is one element in an array its index is 0 and array length is 1. Try objArray[objArray.length -1] and it will work.

Black Bear
  • 230
  • 2
  • 7