0

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

Lightcoder
  • 259
  • 4
  • 14

4 Answers4

3

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 be undefined).

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

Felix Kling
  • 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
  • Well, one is a reference and the other is a tutorial... – Felix Kling Oct 13 '12 at 08:26
  • True... Normally only see the reference ;) – mplungjan Oct 13 '12 at 08:26
2

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:

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

  2. It's not susceptible to someone overwriting the Array symbol (which they can do) with something else.

  3. It's more concise and expressive (this is probably the most subjective of the reasons).

More reading:

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

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)

Riz
  • 9,703
  • 8
  • 38
  • 54
  • 2
    however 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
  • hence I also the tutorial which was also posted by Felix – mplungjan Oct 13 '12 at 08:39
0

Syntax Reference

[element0, element1, ..., elementN]
new Array(element0, element1, ..., elementN)
new Array(arrayLength)

MDN Tutorial

mplungjan
  • 169,008
  • 28
  • 173
  • 236