Javascript Array - Create an Array in Javascript
There are a couple ways to create a Javascript Array.
One way to create an array:
i=new Array("Hi", "Hello", "Cheers", "Greetings");
document.write(i);
document.write("<br>");
document.write(i[1]);
Which Prints:
Hi,Hello,Cheers,Greetings
Hello
-We named our array
i because we hate typing long names.
-We used Javascript's built-in array object constructor
new Array
-We enclosed each piece of our array in quotation marks and separated by commas.
-We called Javascript's built-in
document.write command to print our array to the browser. (By default, array pieces are separted by commas so they are printed too)
-We put in an
HTML line return.
-We printed our array's index number 1.
NOTE: Index numbers that access the pieces of an array start with 0.
Another way to create an array:
i=new Array();
i[0]="Hi";
i[3]="Cheers Mate";
document.write(i);
Which Prints:
Hi,,,Cheers Mate
-We named our array
i because we type poorly.
-We used Javascript's built-in array object constructor
new Array
-This time however, we leave the array blank to start.
-We insert items into our array. We use index numbers to specify where we want the items placed. For our example, we left 1 and 2 blank. Remember, index numbers start at 0.
-We called Javascript's built-in document.write command to print our array to the browser. (By default, array pieces are separted by commas so they are printed too showing our blank pieces.)
NOTE: We could have said
i=new Array(6), adding a number in the parenthesis, if we had wanted to start our array with six pieces. Then we could add more later if we needed to.
Free Copy and Paste Javascript Code:
<script type="text/javascript">
<!--
i=new Array("Hi", "Hello", "Cheers", "Greetings");
document.write(i + "<br>" + i[2] + "<br>");
i=new Array();
i[0]="Hi";
i[3]="Cheers Mate";
document.write(i);
-->
</script>
Resources:
Splitting an Array using Javascript
Javascript Concat - Use Concat to Join Two Arrays in Javascript
Javascript Join - Use Join to Make an Array into a String in Javascript
Javascript Splice - Use Splice to Replace Items in an Array in Javascript
Javascript Reverse - Use Reverse on Items in an Array in Javascript
Javascript Sort - Use Sort on Items in an Array in Javascript
Javascript Shift - Use Shift to Remove Items from the Beginning of an Array in Javascript
Javascript Pop - Use Pop to Remove Items from the End of an Array in Javascript
Javascript Push - Use Push on an Array in Javascript
Javascript Unshift - Use Unshift to Put Items on the Front of an Array in Javascript
Javascript Slice - Use Slice to Get Items from an Array in Javascript
A Very Useful Array Tutorial on a Different Code Site
And Another Useful Array Tutorial on a Different Code Site
PRINT to Browser with document.write Command in Javascript
Make a Line Return Using HTML
Javascript Alert
onClick Javascript
Javascript Referrer
Javascript Reset Form
Annoy Users with onBlur in Javascript
Get Query String Using Javascript
Use innerHTML to Change Text on a Page
Click HERE to comment or discuss at iLoveTheCode GOOGLE Group
| | Dojo Matthew A. Russell... | |
| | CSS David McFarland | |
| | | |