1

What is the correct way on instantiate empty array in javascript?

var array=new Array();
var array=new Array;
var array=[];

All of them look same. Is there any difference?

1
  • no difference... with the very lines you typed Commented Jun 18, 2014 at 0:06

2 Answers 2

3

var array=[];

works best.

Ref - http://jsperf.com/array-instantiation

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

Comments

1

There is one more way:

var array = Array() ;

They all will produce the same result, an empty object Array. I think the best way is using the array literal shortcut [] as it is what use less characters, it is what most people do.

Note that the same does not apply in the case of Boolean, Number and String, as those constructors will return primitives of type 'object' (object Boolean, object Number and object String respectively) while their literals will return primitives of types 'boolean', 'number' and 'string' respectively (and will behave differently in some situations), but array primitive type it is 'object' in any case.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.