Understanding Array Initialization in JavaScript

  • Context: Java 
  • Thread starter Thread starter AllenHe
  • Start date Start date
  • Tags Tags
    Javascript
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
1 reply · 2K views
AllenHe
Messages
74
Reaction score
0
The general form for creating an array is:

Myarray=new arrayname();

But I don't understand wht should I type in the name of the array twice? Because I thought Myarray and arrayname is the same.
 
Physics news on Phys.org
DO you really mean something like this:

MyArray myArray = new MyArray();

where MyArray is a class and myArray is an instance?

The statement above creates an instance called myArray of type MyArray. This is how you initialize myArray rather like assigning a value to it.

MyArray myArray1;
MyArray myArray2;
MyArray myArray3;you can't use myArray1, myArray2, or myArray3 until you've assigned them a value that is until you do this:

myArray1 = new MyArray();
myArray2 = new MyArray();
myArray3 = new MyArray();

hope this helps.