javascript tutorial - [Solved-5 Solutions] Difference between “array()” and “[]” while declaring a javascript array - javascript - java script - javascript array



Problem:

What's the real difference between declaring an array like this:

var myArray = new Array();
click below button to copy the code. By JavaScript tutorial team

and

var myArray = [];
click below button to copy the code. By JavaScript tutorial team

Solution 1:

  • There is a difference, but there is no difference in that example.
  • Using the more verbose method: new Array() does have one extra option in the parameters: if we pass a number to the constructor, we will get an array of that length:
x = new Array(5);
alert(x.length); // 5
click below button to copy the code. By JavaScript tutorial team

To illustrate the different ways to create an array:

var a = [],            // these are the same
    b = new Array(),   // a and b are arrays with length 0

    c = ['foo', 'bar'],           // these are the same
    d = new Array('foo', 'bar'),  // c and d are arrays with 2 strings

    // these are different:
    e = [3]             // e.length == 1, e[0] == 3
    f = new Array(3),   // f.length == 3, f[0] == undefined

;

click below button to copy the code. By JavaScript tutorial team

Solution 2:

  • The difference between creating an array with the implicit array and the array constructor is subtle but important.
  • When we create an array using
var a = [];
click below button to copy the code. By JavaScript tutorial team
  • You're telling the interpreter to create a new runtime array. No extra processing necessary at all. Done.
  • If we use:
var a = new Array();
click below button to copy the code. By JavaScript tutorial team
  • You're telling the interpreter, we want to call the constructor "Array" and generate an object. It then looks up through our execution context to find the constructor to call, and calls it, creating our array.
  • We may think "Well, this doesn't matter at all. They're the same!". Unfortunately we can't guarantee that.
  • Take the following example:
function Array() {
    this.is = 'SPARTA';
}

var a = new Array();
var b = [];

alert(a.is);  // => 'SPARTA'
alert(b.is);  // => undefined
a.push('Woa'); // => TypeError: a.push is not a function
b.push('Woa'); // => 1 (OK)
click below button to copy the code. By JavaScript tutorial team
  • In the above example, the first call will alert 'SPARTA' as you'd expect. The second will not. We will end up seeing undefined. You'll also note that b contains all of the native Array object functions such as push, where the other does not.
  • While we may expect this to happen, it just illustrates the fact that [] is not the same as new Array().
  • It's probably best to just use [] if we know we just want an array. We also do not suggest going around and redefining Array...

Solution 3:

Oddly enough, new Array(size) is almost 2x faster than [] in Chrome, and about the same in FF and IE (measured by creating and filling an array). It only matters if we know the approximate size of the array. If we add more items than the length you've given, the performance boost is lost.

Solution 4:

The first one is the default object constructor call. We can use it's parameters if we want.

var array = new Array(5); //initialize with default length 5
click below button to copy the code. By JavaScript tutorial team

The second one gives we the ability to create not empty array:

var array = [1, 2, 3]; // this array will contain numbers 1, 2, 3.

click below button to copy the code. By JavaScript tutorial team

Solution 5:

In order to better understand [] and new Array():

> []
  []
> new Array()
  []
> [] == []
  false
> [] === []
  false
> new Array() == new Array()
  false
> new Array() === new Array()
  false
> typeof ([])
  "object"
> typeof (new Array())
  "object"
> [] === new Array()
  false
> [] == new Array()
  false
click below button to copy the code. By JavaScript tutorial team

The above result is from Google Chrome console on Windows 7.


Related Searches to javascript tutorial - Difference between “array()” and “[]” while declaring a javascript array