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
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
First, if you find yourself writing
new Array, there’s probably a better way to do it. But first I’ll explain theArrayconstructor and then talk about better ways.The
Arrayconstructor 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:
…creates an array with a
lengthof5which 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, thelengthis0.The second form:
…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:
It’s almost always much better to use array literals.
The first form above, done with an array literal instead:
The second form above, done with an array literal instead:
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
Arrayconstructor has. For instance, what does this do?Answer: It depends (on what
foohas it in it, a number or something else). This is just Not Goodtm.It’s not susceptible to someone overwriting the
Arraysymbol (which they can do) with something else.It’s more concise and expressive (this is probably the most subjective of the reasons).
More reading:
Arrays