2

I'm wondering about why isn't advisable to use array declaration by:

 var arr = new Array() 

as I thought declaring by [] was supposed to be protected in case of overwrite Array but...

Array = 1;
var arr = [] // boom  
TypeError: Cannot read property 'slice' of undefined

Personally, I prefer using var arr = [], but now I don't know what is the advantage of using [] instead of Array except that write faster.

abuduba
  • 4,986
  • 7
  • 26
  • 43

6 Answers6

4

Both methods are interchangeable (regarding functionailty), except for one case:

These are not equivalent:

var array = new Array(3);  // <-- Creates an array with length 3
var array = [3];

//If you wanted to use the `Array` to create an array with one element, use:
var array = new Array();
array[0] = 3;

[] is not only shorter, but also consistent. The Array constructor is only useful for creating arrays with an initial length.

Rob W
  • 341,306
  • 83
  • 791
  • 678
  • Note `[]` is favoured because 1) it minimizes better and 2) `new Array()` has a functional call overhead where as `[]` can be optimized better internally in javascript engines. – Raynos Jan 07 '12 at 15:51
4

as I thought declaring by [] was supposed to be protected in case of overwrite Array but...

It is under EcmaScript 5 but not under EcmaScript 3, so it won't be on older browsers.

The relevant portion of the spec is http://es5.github.com/#x11.1.4

  1. Let array be the result of creating a new object as if by the expression new Array() where Array is the standard built-in constructor with that name.

The bolded text was added in EcmaScript 5 but was not present in EcmaScript 3.

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
2

There's already lots of similar posts on SO.

See this:

Javascript array best practice to use [] instead of new array?

And this:

Use of JavaScript new Array(n) Declaration

Community
  • 1
  • 1
Martin Dandanell
  • 831
  • 1
  • 7
  • 20
  • Instead of posting this as an answer, you can flag the answer for attention, and say "Duplicate of: ". When you get the privilege to add comments, this would also be suitable for a comment. – Rob W Jan 07 '12 at 15:46
  • Will do - Thanks for the heads up! – Martin Dandanell Jan 07 '12 at 16:11
0

IMO, it doesn't really matter you use new Array() or =[]. Because, most of today's browsers' Javascript engines are smart enough to take advantages of small implementation differences. I frequently use =[] just because of shave off some extra bytes from the javascript source.

Osman Turan
  • 1,361
  • 9
  • 12
0

I ran a test. It appears that if you override window.Array with anything except another function it won't work.

TJR
  • 3,617
  • 8
  • 38
  • 41
-5

[] it doesn't mean anything in fact Javascript doesn't explicitly mention anything like that. so you have to follow the variable declaration rules as its standard mentions.

user1120193
  • 252
  • 3
  • 11
  • don't waste your time with this I suggest you move up and learn rest of concepts of Javascript . – user1120193 Jan 07 '12 at 15:44
  • It still doesn't address the question, though; maybe expand on what you're trying to say a little. – Dave Newton Jan 07 '12 at 15:47
  • I just show the direction and you have to travel by yourself. – user1120193 Jan 07 '12 at 15:51
  • I am not here for those tiny pixelated badges and reputations. perhaps people like you can have some fun thumbing down on me but I don't care. – user1120193 Jan 07 '12 at 16:11
  • @Gryphes I did not *vote down*, because `-3` is enough to fade an answer. I encourage wrong answers to be deleted, so that SO remains clean. – Rob W Jan 07 '12 at 16:14