1

Is the object whiteStripes the same exact thing in both cases?

var whiteStripes = {'Jack' : 'White', 'Meg' : 'White'};

var whiteStripes = new Array();
whiteStripes['Jack'] = 'White';
whiteStripes['Meg'] = 'White';
Adam Szabo
  • 11,302
  • 18
  • 64
  • 100
user784637
  • 15,392
  • 32
  • 93
  • 156

2 Answers2

4

While you will still be able to access the properties the same way (whiteStripes['Jack']) in both instances when you declare whiteStripes = new Array(); you are saying it has all the properties and attributes of an array like length for example. If you are not intending to use it as a true array (pop, push, length, etc.) then don't use a JavaScript array.

Ian
  • 50,146
  • 13
  • 101
  • 111
Andy Meyers
  • 1,561
  • 1
  • 15
  • 21
3

No, it's not the exact same thing.

Both will work, because an array is also an object, but if you want just an object you should not create an array to get one.

These would result in the exact same thing being created:

var whiteStripes = {'Jack' : 'White', 'Meg' : 'White'};

var whiteStripes = new Object();
whiteStripes['Jack'] = 'White';
whiteStripes['Meg'] = 'White';
Guffa
  • 687,336
  • 108
  • 737
  • 1,005