iLTC Home
Bookmark iLTC!
iLTC Google Boards

Google Groups
Receive The Code in Email!



Categories

Perl
  • Perl Tutorials Beginners

    Javascript
  • Javascript Tutorials
  • Javascript Tutorials Beginners

    HTML
  • HTML Tutorials



  • Pass the Javascript, please...
    20,113

    iLoveTheCode.com
    Javascript > Javascript Tutorials How To Easy > Javascript Array Create an Array in Javascript

    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
    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





    jQuery in ActionjQuery in Action

    Bear Bibeault, Yeh...

    New $26.39

    DojoDojo

    Matthew A. Russell...

    New $26.39

    Bulletproof Web DesignBulletproof Web Design

    Dan Cederholm

    New $26.39

    Learning Web DesignLearning Web Design

    Jennifer Niederst ...

    New $29.69

    CSSCSS

    David McFarland

    New $23.09

    HTML, XHTML, and CSS, Si...HTML, XHTML, and CSS, Sixth Edition ...

    Elizabeth Castro

    New $23.09

    Struts 2 in ActionStruts 2 in Action

    Don Brown, Chad Da...

    New $29.69

    JavaScriptJavaScript

    David Flanagan

    New $31.49

    Dreamweaver CS3Dreamweaver CS3

    David McFarland

    New $29.69


    Of Interest:
  • Get the Length of a Word or String in Perl
  • To See if a Directory Exists in Perl
  • Making a Directory Using mkdir in Perl
  • Matching with a Regular Expression, REGEX, in Perl
  • Javascript Superscript Text
  • Javascript Italics Text
  • Make Text Blink Using Javascript
  • Javascript Uppercase Text
  • HTML Forms
  • HTML Superscript Text
  • Make Italic Text Using HTML
  • HTML Body Tag

  • More Articles:
  • Write a FOR LOOP in Perl
  • Write a FOREACH LOOP in Perl
  • Create an Array of Specific Numbers in Perl
  • Undefine an Array in Perl
  • Javascript Alert
  • onClick Javascript
  • Setting a Cookie using Javascript
  • Use onLoad in Body Tag with Javascript
  • Javascript Shift - Use Shift to Remove Items from the Beginning of an Array in Javascript
  • Close Window with window.close in Javascript
  • HTML Background
  • HTML Superscript Text
  • Make Strikethrough Text Using HTML
  • Make Italic Text Using HTML

  • Search iLTC w/ Google





    Recent Articles

  • Javascript Focus Form Field
  • Javascript Lowercase Text
  • Javascript Uppercase Text
  • Javascript Superscript Text
  • Javascript Subscript Text
  • Make Text Blink Using Javascript
  • Make a String into a Link in Javascript
  • Javascript Italics Text
  • Javascript Bold Text
  • Javascript Redirect
  • HTML Frames
  • HTML Color Chart
  • HTML Link
  • Javascript Status Bar








  • Perl Programming - Javascript Programming - Maybe Some PHP - Free Scripts - ILoveTheCode
    All Content © 2006 iLoveTheCode.com


    Contact Chris | About this Site


    316,853
    Counter by iLoveTheCode.com


    Last Modified: Friday, 23-Jun-2006 07:23:14 EDT