The Basics

Variables

If you don't get the hang of any of the other Javascript lingo right away, the two most important ones to know are variables and functions. We'll sneak more terms in later.

Think of variables as the raw materials and the functions as little machines that use those raw materials to make or do something new as their output, like put up an alert box or add some numbers together, for example.

Variables can be either words or numbers. A word or a series of words are referred to as a string in the programming world. Variables can either be preset in the Javascript, set by some user action (like clicking on something), input from a form, or some data that Javascript can pick up on its own (like the date and time on the user's computer).

Functions DO things. They don't just lie about like their lazy counterparts, the variables. They have goals in life - to accomplish things. Their actions can range from very simple to quite complex. We won't get too complex yet, so don't worry. The functions very often use variables to conduct their little operations. Functions can either be triggered by some user action, like submitting a form, placing their cursor over some object, or automatically, like when the page is done loading, for example. These are referred to as events and we'll discuss them a bit later.

Variables

The first thing we need to do when we make a variable is to declare our variables by typing "var", the name of our variable, then "=", then whatever value we want to assign it. This formally introduces the new variable to all its friends in the Javascript party going on inside the web page. After a variable is formally introduced, it can be used without the "var" and "=" attached to it. They're kind of on first name basis after that.

var firstPrez = "George Washington";

var lastPrez = "George W. Bush";

Something you'll notice about the names of the variables above - the two words "last" and "Prez" are combined, using lower case on the first word and upper case on every word after it. This isn't absolutely necessary, but it's kind of a convention among programmers, so you'll want to do it too, so you'll be considered cool. Don't use ANY spaces between words, though - no, no, no.

We can also make a variable by combining other strings and variables like this:

var newString = "Our first president was " + firstPrez + " and our last was " + lastPrez + ".";

When Javascript substitutes the values for firstPrez and lastPrez we'll get:

"Our first president was George Washington and our last was George W. Bush."

You'll also note that in the declared variable above, there are spaces after the word "was" and before the word "and" and also after the second "was". If we didn't put spaces there, when Javascript did its interpretation of the variables, we'd end up with this:

"Our first president wasGeorge Washingtonand our last wasGeorge W. Bush."

The words would run together, people could get injured, and the whole thing could get ugly. So remember to use spaces where you want words to be separated because Javascript is good, but it can't read your mind.

A few other things to notice about variables:

  • The new variable is on the left and its value is on the right.
  • It is followed by a semi-colon. The semi-colon is a good habit to get into using. It's like putting a period after a sentence. It tells the script where one statement ends and another begins.
  • The value, if it's a word or group of words (a string), will be contained in quotes, unless it is using another variable as its value or if it's just a number. Single quotes or double quotes are okay as long as you're consistant.
  • In addition to strings and numbers, variable values can be either true or false (called a Boolean). Booleans kind of act like a light switch - they're either on or off - true or false. They are often displayed as either a 1 (true) or 0 (false).

Now let's look more at Functions...