RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


JavaScript QuickStart: Elements of JavaScript : Page 2

JavaScript is cropping up everywhere and if you want to learn to use it, you'll need to understand these essential elements.

Variables are containers that hold a value. You'll see variables in use throughout a script.

Here's a few notes about variables:

  • Variables can have any name, as long as the name starts with a letter.

  • The process of creating a variable is called "declaring the variable."

    A common JavaScript error is "variable-name undefined." That means you are trying to use a variable in the script, except that particular variable doesn't exist.

  • You can assign a specific value to a variable. This example declares a variable named "clickThrus" and assigns it the value 0:

    var clickThrus = 0

  • You can have the script calculate the value of a variable. This example declares a variable named visits, and sets the value of "visits" to be the sum of "clickThurs" plus 10:

    var visits = clickThrus + 10

Arrays are a set of variables referred to by a single variable name. If you're working with a series of mouseovers (images or phrases that change when the reader moves his or her mouse over some part of your page), you might see an array that looks something like this:

blurb = new Array(5)

blurb[0] = "Your mouse is over Halebo, photographed from the Hubbel Space Telescope!"
blurb[1] = "Your mouse is over the Andromeda galaxy."
blurb[2] = "Now you're mousing on the planet Venus."
blurb[3] = "Last, but not least, is Neptune."
blurb[4] = ""

This array creates a set of five variables; each of the variables contains a different phrase. The whole set -- the array -- is named "blurb."

Arrays start numbering their variables with 0, so the the individual variables within the array are named blurb[0], blurb[1], blurb[2], blurb[3], and blurb[4]. In this example, blurb[4] has two double quote marks with no text; this will make the status bar blank.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date