1. Define an array object to store a series of values in a separate variable name. We use the keyword new to create an array object. The following code defines an array object named myArray: varmyArraynewArray () or: varshowText []; // creates an array showText. push (helloworld); // Number of push directions
1. Define an array
An array object is used to store a series of values in a separate variable name.
We use the keyword new to create an array object. The following code defines an array object named myArray:
Var myArray = new Array ()
Or:
Var showText = []; // create an array
ShowText. push ('Hello World'); // push adds one or more elements to the end of the array and returns a new length.
There are two ways to assign values to an array (you can add any number of values, just as you can define any number of variables you need ).
1.
Var mycars = new Array ()
Mycars [0] = "Saab"
Mycars [1] = "Volvo"
Mycars [2] = "BMW"
You can also use an integer independent variable to control the array capacity:
Var mycars = new Array (3)
Mycars [0] = "Saab"
Mycars [1] = "Volvo"
Mycars [2] = "BMW"
2. var mycars = new Array ("Saab", "Volvo", "BMW ")
2. Access the Array
By specifying the array name and index number, you can access a specific element.
The following is the code line:
Document. write (mycars [0]) is output below: Saab
To modify the values in an existing array, you only need to add a new value to the specified label:
Mycars [0] = "Opel"; now, the above Code: document. write (mycars [0]); will output: Opel
Iii. Common Methods:
1. For... In statement
Use the for... in Declaration to cyclically output the elements in the array.