How to Use an Array Class in JavaScript
Create an Array object like this: var aValues = new Array(); ; , Pass the array size as a parameter if you know ahead of time how many items it will contain: var aValues = new Array(20); , Use either of these two methods to populate the array with...
Step-by-Step Guide
-
Step 1: Create an Array object like this: var aValues = new Array(); ;
For example, the following line of code creates an array of three strings: var aColors = new Array(“red”, “green”, “blue”); As in strings, the first item in an array is in position 0, the second is in position 1, and so on. , For instance, to output the string “green” from the array defined previously, you do this: alert(aColors); //outputs “green” , Like the same property in strings, the length property is always one more than the position of the last item, meaning that an array with three items has items in positions 0 through
2. var aColors = new Array(“red”, “green”, “blue”); alert(aColors.length); //outputs “3” , This addition changes the length of the array from 3 to
4. ,, For instance, the previous example can be rewritten in the following form: var aColors = ; alert(aColors.length); //outputs “3” aColors= “purple”; alert(aColors.length); //outputs “26” Note that, in this case, the Array class is never mentioned explicitly.
The square brackets imply that the enclosed values are to be made into an Array object.
Arrays declared in this way are exactly equal to arrays declared in the more traditional manner. -
Step 2: Pass the array size as a parameter if you know ahead of time how many items it will contain: var aValues = new Array(20);
-
Step 3: Use either of these two methods to populate the array with bracket notation
-
Step 4: similar to how it is done in Java: var aColors = new Array(); aColors= “red”; aColors= “green”; aColors= “blue”;
-
Step 5: Understand that an array dynamically grows in size with each additional item.
-
Step 6: Specify values as arguments
-
Step 7: creating an Array object with a length equal to the number of arguments.
-
Step 8: To access a particular item
-
Step 9: use square brackets enclosing the position of the item to retrieve.
-
Step 10: Determine the size of an array with the length property.
-
Step 11: To add another item to the array defined previously
-
Step 12: place the value in the next open position
-
Step 13: since the array grows and shrinks automatically: var aColors = new Array(“red”
-
Step 14: “green”
-
Step 15: “blue”); alert(aColors.length); //outputs “3” aColors= “purple”; alert(aColors.length); //outputs “4” In this code the next open position is 3
-
Step 16: so the value “purple” is assigned to that position.
-
Step 17: Place a value in position 25 of this array and ECMAScript fills in all positions from 3 to 24 with the value null
-
Step 18: and then places the appropriate value in position 25
-
Step 19: increasing the size of the array to 26: var aColors = new Array(“red”
-
Step 20: “green”
-
Step 21: “blue”); alert(aColors.length); //outputs “3” aColors= “purple”; aColors(arr.length); //outputs “26”
-
Step 22: Define an Array object by using the literal representation
-
Step 23: which is indicated by using square brackets () and separating the values with commas.
Detailed Guide
For example, the following line of code creates an array of three strings: var aColors = new Array(“red”, “green”, “blue”); As in strings, the first item in an array is in position 0, the second is in position 1, and so on. , For instance, to output the string “green” from the array defined previously, you do this: alert(aColors); //outputs “green” , Like the same property in strings, the length property is always one more than the position of the last item, meaning that an array with three items has items in positions 0 through
2. var aColors = new Array(“red”, “green”, “blue”); alert(aColors.length); //outputs “3” , This addition changes the length of the array from 3 to
4. ,, For instance, the previous example can be rewritten in the following form: var aColors = ; alert(aColors.length); //outputs “3” aColors= “purple”; alert(aColors.length); //outputs “26” Note that, in this case, the Array class is never mentioned explicitly.
The square brackets imply that the enclosed values are to be made into an Array object.
Arrays declared in this way are exactly equal to arrays declared in the more traditional manner.
About the Author
Gloria Ramos
Dedicated to helping readers learn new skills in lifestyle and beyond.
Rate This Guide
How helpful was this guide? Click to rate: