1
var array = new Array()
array.name = 'peter'
console.log(array.length) //returns 0
console.log(array.name) //returns peter

I would've expected the length to be 1 and not 0. Can anyone explain what is going on here?

2 Answers2

2

You need to push value to array.

array.push({name:'peter'});

Nonik
  • 645
  • 4
  • 11
1

That merely add a property name to instance of that array object. To actually add item to the array to increase its size, you need to push item into it. Something like, array.push('peter').

noobius
  • 1,529
  • 7
  • 14