Since I am into the basics of JavaScript I am able to create array, but I want to know what we might include in the parenthesis of array like test=new Array("parameter?"); so I meant what to include as parameter of that javascript array
-
Are you wanting to create an array with some existing items as the initial contents? – nick_w Oct 13 '12 at 08:16
-
http://stackoverflow.com/questions/6949162/how-does-parentheses-work-in-arrays-javascript – NullPoiиteя Oct 13 '12 at 08:16
4 Answers
The array constructor is overloaded and which result you get depends on how many arguments you pass and of which data type they are:
More than one argument (any type):
var arr = new Array(1,2,3,4);
Creates a new array containing the values passed to the constructor.
One argument ...
... of type number:
var arr = new Array(4);
Creates an empty array and sets its length to the number passed as argument. The array appears to have 4 elements (because its length is 4) but it does not contain any elements (e.g.
arr[0]
will beundefined
).... any other type:
var arr = new Array('foo');
Creates an array containing one element, the argument passed to the constructor (the same as passing multiple arguments).
Because of this ambiguity of the Array
constructor function, you should avoid it and use array literals instead:
var arr = [1,2,3,4];
That said, to learn the basics, read the MDN JavaScript Guide, especially about arrays.

- 795,719
- 175
- 1,089
- 1,143
-
Strange there is such a difference between [this](https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Predefined_Core_Objects#Array_Object) page and [this](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array) one. – mplungjan Oct 13 '12 at 08:25
-
-
First, if you find yourself writing new Array
, there's probably a better way to do it. But first I'll explain the Array
constructor and then talk about better ways.
The Array
constructor has two forms: A form that takes zero or one arguments (if that one argument is a number), and a form that takes more than one argument (or just one argument if the argument is not a number). Never use the second form, and avoid using the first one.
The first form:
var a = new Array(5);
...creates an array with a length
of 5
which has no entries (JavaScript arrays are "sparse," because they're not really arrays at all — more on that below). If you don't supply any argument at all, the length
is 0
.
The second form:
var a = new Array(5, 6);
...creates an array with as many entries as there are arguments (so, two in this case) and uses the arguments to set the values of those entries.
Note that both the number and type of arguments is important:
var a = new Array(5); // Creates an array with length = 5 with no entries
var b = new Array("5"); // Creates an array with length = 1 and entry [0] = "5"
It's almost always much better to use array literals.
The first form above, done with an array literal instead:
var a = [];
a.length = 5;
The second form above, done with an array literal instead:
var a = [5, 6];
Using array literals is "better" (in my view) for several reasons, some of which are more subjective than others:
It's clearer, and doesn't have the ambiguity that the
Array
constructor has. For instance, what does this do?var a = new Array(foo);
Answer: It depends (on what
foo
has it in it, a number or something else). This is just Not Goodtm.It's not susceptible to someone overwriting the
Array
symbol (which they can do) with something else.It's more concise and expressive (this is probably the most subjective of the reasons).
More reading:
- The ECMAScript (JavaScript) specification on
Array
s - A myth of arrays (in my blog), which explains how JavaScript arrays aren't arrays

- 1,031,962
- 187
- 1,923
- 1,875
You are in learning curve. See here. You can write array items there.
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array (Need editorial review)

- 9,703
- 8
- 38
- 54
-
2however http://w3fools.com/ - [MDN](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array) is normally a much better resource - in this case w3schools seems to be fairly ok – mplungjan Oct 13 '12 at 08:19
-
[element0, element1, ..., elementN]
new Array(element0, element1, ..., elementN)
new Array(arrayLength)

- 169,008
- 28
- 173
- 236