Understanding Array Initialization in JavaScript

  • Java
  • Thread starter AllenHe
  • Start date
  • Tags
    Javascript
In summary, the general form for creating an array is to use "new" followed by the name of the array with parentheses. However, if the array is a class, the syntax is slightly different and involves creating an instance of the class. This can be done by using the class name followed by the instance name and setting it equal to "new" and the class name with parentheses. It is important to initialize the instance before using it.
  • #1
AllenHe
74
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.
 
Technology news on Phys.org
  • #2
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.
 

1. What is a question in JavaScript?

A question in JavaScript refers to a statement or prompt that requires a user to provide input or information, which is then processed by the JavaScript code to produce a desired result or action.

2. How do I create a question in JavaScript?

To create a question in JavaScript, you can use the built-in prompt() function. This function takes in a message or question as an argument and displays it to the user, allowing them to input a response. The input is then stored in a variable, which can be used in your code for further processing.

3. Can I customize the appearance of a question in JavaScript?

Yes, you can customize the appearance of a question in JavaScript by using HTML and CSS. You can create a form with a label and input field, or use a div element with styling to make it look like a question. However, the functionality of the question will still rely on the prompt() function.

4. How do I handle user input from a question in JavaScript?

You can handle user input from a question in JavaScript by storing their response in a variable and using conditional statements to check for specific values or conditions. You can also use this input to perform calculations, update HTML elements, or trigger other functions.

5. Are there any alternatives to using a question in JavaScript?

Yes, there are alternatives to using a question in JavaScript. You can use other input methods such as confirm() for yes or no questions, alert() to display a message, or prompt() with pre-defined options. You can also create custom forms with HTML and use JavaScript to validate the input.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
6
Views
979
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
9
Views
999
  • Programming and Computer Science
Replies
4
Views
750
  • Programming and Computer Science
Replies
21
Views
5K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
12
Views
2K
Back
Top