Variables..what are they?

In JavaScript a variable is a placeholder for a value.  An example would be:

 
var MyValue = 5;

  "MyValue" is the variable and it has been assigned the value of 5.

OK, so what's a variable?

Variables..why you need them.

Variables allow our JavaScript programs to work with all kinds of numbers, words, and conditions.  For example:

 
var Number1 = 5;
  var Number2 = 4;
  var Answer1;

  Answer1 = 5 + 4;

Here variables were used to do an addition problem.

Here is another example of using variables:

 
var Name;
  Name = prompt("What is your name?");
document.write("Hi ", Name);

  The above JavaScript code will cause a prompt box to appear asking the user for their name.  If the user types in Tom, the browser will then display:

   
Hi Tom

Variables..rules for making them.

Variables are sometimes called identifiers because they identify a specific value or condition.

A JavaScript identifier must start with a letter or underscore ("_"); subsequent characters can be the underscore and also be digits (0-9). Because JavaScript is case sensitive, letters include the characters "A" through "Z" (uppercase) and the characters "a" through "z" (lowercase).  No spaces or other characters are allowed.

Variables..examples...

Legal JavaScript variables would be:

var MyName;
var Your_Name;
var _My_Very_Special_Value_of_all_time;
var X;
var XandY;
var Value1;
var vAlUe25;

Generally you want to make your variables descriptive - That is use Name instead of X to hold a user's name.

Contents Copyright © 2000, Adamson House, Ltd.