iLTC Home
Bookmark iLTC!

Categories

Perl
  • Perl Tutorials Beginners

  • Javascript
  • Javascript Tutorials
  • Javascript Tutorials Beginners

  • HTML
  • HTML Tutorials

  • Command Line
  • Command Line Tutorials

  • How Tos
  • Other Tips and Tricks



  • for($i;$i < 1000;$i++) { }
    1716024

    iLoveTheCode.com

    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






    Previous Article: Javascript Push Use Push on an Array in Javascript
    Next Article: Javascript Unshift Use Unshift to Put Items on the Front of an Array in Javascript






    Search iLTC w/ Google




    Recent Articles

  • HTML Font Code
  • Javascript and CSS - Easy Pop Up Help Balloons
  • Use OnMouseOver in Javascript
  • Use onMouseOut in Javascript
  • Use innerHTML to Change Text on a Page
  • Renaming a File with Perl's RENAME Function
  • PRINT to Browser with document.write Command in Javascript
  • Passing an Argument or Variable to a Javascript
  • Opening an Alert Box in Javascript
  • Opening a REMOTE WINDOW in Javascript
  • OPEN and WRITE a Data File in Perl
  • OPEN and READ a Data File in Perl
  • Mimic Typing from a Textfield in a Textarea with Javascript
  • Javascript Reset Form








  • Perl Programming - Javascript Programming - HTML Code - Mac Unix Command Line - Maybe Some PHP - Free Scripts - ILoveTheCode
    All Content © 2006-2009 iLoveTheCode.com


    Contact Chris | About this Site


    1715750
    Counter by iLoveTheCode.com


    Last Modified: Saturday, 10-Jan-2009 05:40:24 EST



    Hosted as well :)
    Home-brewed logo - 1997