Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between condensed arrays and literal arrays?

As the title says: What is the difference between condensed arrays and literal arrays?

new Array("John", "Bob", "Sue"); // condensed array

["John", "Bob", "Sue"]; // literal array

Are there things I can do with one that I can't do with the other? Or is it the way it is kept in the memory?

like image 483
TCS Avatar asked Sep 17 '11 04:09

TCS


1 Answers

No, there is no difference in the produced object, they are the same.

The first is an attempt to satisfy programmers that are use to a "classical" environment where you have to instantiate a "new" Array object.

It should be noted that Arrays in javascript are not a sequential allocation of memory, but objects with enumerable property names and a few extra (useful) methods.

Because of this creating an array of a set length is fairly useless and unnecessary in most (if not all) cases.

var array = new Array(10);

is functionally the same is manually setting the length of your array

var array = [];
array.length = 10;
like image 98
jondavidjohn Avatar answered Oct 12 '22 09:10

jondavidjohn