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.

0

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

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.
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

2 Comments

Instead of posting this as an answer, you can flag the answer for attention, and say "Duplicate of: <other question(s) here>". When you get the privilege to add comments, this would also be suitable for a comment.
Will do - Thanks for the heads up!
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.

Comments

0

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

Comments

-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.

5 Comments

don't waste your time with this I suggest you move up and learn rest of concepts of Javascript .
It still doesn't address the question, though; maybe expand on what you're trying to say a little.
I just show the direction and you have to travel by yourself.
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.
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.