The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Array Length Property

Use the array length property to display an opening greeting to the user indicating how many original colonies (elements in the array) there were.

I am really confused how to use this property. I was trying to follow an example, but I just can't get it, please help.

"There were thirteen original American colonies. They were Connecticut, Delaware, Georgia, Maryland, Massachusetts, New Hampshire, New Jersey, New York, North Carolina, South Carolina, Pennsylvania, Rhode Island and Virginia."

<script type="text/javascript">
place = new Array(
"",
"There were thirteen original American colonies.",
"Massachusetts, New Hampshire, New Jersey, New York,",
"Rhode Island and Virginia."
);

Instead of individually declaring your elements and values, this format lets you declare everything in one fatal swoop. This saves space in the long run, making for faster downloads (the difference is somewhat minute in this case, but still it's a good idea to to get accustomed to things like this, it helps in the long run).

Code:

for (var i=0, colony; colony=colonies[i]; i++) {
alert(colony);
}

So here is a bit advanced playoff of the for loop. The first segment initializes loop variables. In this case, it's initializing the counter variable AND a loop scope variable to make reading the code more manageable.

The next segment is the condition to check against for stopping the loop. In this case, it tries to index the array element referenced to by i. If i is an invalid reference, then it can't assign a value to colony, and the loop breaks off. If it IS a valid reference, it's assigned to colony to be used within the loop.

The last segment is just the standard loop increment, increases i so the loop check segment can see if it needs to break out or not.