Displaying Array Items

By: Dusty Arlia
Published on September 24, 2012
Last Updated on Tuesday, July 07, 2015 at 9:50 PM
Total Updates: 6

When you are trying to display array items on your webpage, you must use the document.write() method to "print" your array item to the screen. To access your array item, use its index number. For example:

shoppingCart[3]

This accesses your fourth item in your array. This is because your first item will have an index number of 0.

Now to print that fourth item in your array named shoppingCart use this code:

var shoppingCart['ice cream', 
                 'eggs',
                 'milk',
                 'cereal',
                 'bananas',
                 'bread'];         //This declares (creates) your array and assigns it values


document.write(shoppingCart[3]);       //prints "cereal" to the screen

Comments:

Ad: