Working with variables

A variable is a named element used to store and retrieve information. With variables, we can store information in one part of the program, and access information from that variable in another part of the program.

Like many other programming languages, JavaScript has variables. You can think of variables as empty containers. You can place data into those containers and then use (or refer to) those contains by their name.

Form more information about variables, access

  • Discussion of data types (types of variables supported in JavaScript)
  • Declaring variables in JavaScript
  • Assigning values to variables

Declaring variables in JavaScript

Before you use a variable, you should declare (create) it. The process of creating a variable is also known as declaring a variable. When you declare a variable, you tell the computer to reserve some memory for your program to store some data. To declare a variable in JavaScript, use the var command. For instance, the following command creates a variable

var age;

called age. With the above command, computer will reserve some memory and allow you to store to or retrieve from that memory with the name you chose, age. Remember at this point our variable age is null or has a garbage value–whatever computer’s memory slot happens to hold.

You can declare more than one variable by separating each with a comma and still use one var command, as:

var state, city, zip, country;

When you declare a variable, keep the following JavaScript restrictions in mind to a variable name:

  1. A variable name cannot be one of the reserved words in JavaScript. Examples of reserved words in JavaScript include var, or document.write. Basically, reserved words have a specific purpose in JavaScript, they cannot be used as a variable name.
  2. The first letter of a variable name must be a letter or an underscore (_). age, year, _month are all valid variable names. However, 11month, 9_ are not valid variable names.
  3. A variable name cannot contain any space characters. If your variable consists of more than one word, separate each word with a underscore (i.e., first_name, last_name) or make the first letter of each word a capital letter (i.e., FirstName, LastName).

Remember that variable names are case-sensitive. In JavaScript, a variable named first_name is considered different from first_Name or FIRST_name.

Also, keep your variable names meaningful. Avoid using a variable name such as k, l, m, z, or a because you may have hard time figuring out what each variable represent in the future or in a long program.

Declaring variable without var command

As noted above, to explicitly declare (create) a variable in JavaScript we use the var command. In JavaScript, however, you can also declare variables implicitly by using the assignment operator to assign a value to the new variable. For example,

age = 50;

declares a variable called age and signs he integer value 50 to this variable.

The following shows some more examples of variables being created when a value is assigned to a variable:

<script language="javascript">
sum = x = y = 0; // declares three variables and each is set to 0.
</script>

Alternatively, you may declare and assign the values using the var command:

<script language="javascript">
var sum, x, y; // declares multiple variable with the var command.
sum = x = y = 0; // sets each variable to 0
</script>

So after reviewing the examples above you might be wondering what approach should you take to create variables? Well, as a matter of good programming practice, you should explicitly declare variables with the var command – before using. Explicitly declaring a variable helps the browser efficiently and accurately process and manage the variables.

Assigning values to variables

After you declare a variable, you can assign a value to a variable. Assigning a value to a variable means storing a value to a variable. To assign a value, use the equal sign:

var age;
age = 55;

The first line declares a variable called age. The second line stores the number 55 to our variable age. If you want, you could also combine those two lines into one, as :

age = 55;

In this one line above, we declare a variable called age when we assign the value 55. In this case, the value is declared implicitly; to explicitly declare a variable, use the command var before a variable name.

Consider the following example:

<script language="javascript">
var fName, age;
fName = "John";
age = 23;
document.write (fName);
document.write ("is");
document.write (age);
</script>

On the first line, we indicate that it is a JavaScript code. The second line declares two variables. On the third line (fName = “John”;), we assign a string value to the variable fName. On the fourth line, we assign a numerical value to the variable age. As the line 5 (document.write (fName);) shows, to print a value of a variable, simply type the exact variable name with document.write () method. Line 7 prints the word “is” and line 8 prints the value, 23, of the variable age. The following shows the output of the above code.

Johnis23

To print “Johnis23” as “John is 23”, simply add a space character around the word “is”, as

<script language="javascript">
var fName, age;
fName = "John";
age = 23;
document.write (fName);
document.write (" is ");
document.write (age);
</script>

That will print:

John is 23

We could simplify our code to these fewer lines:

<script language="javascript">
fName = "John";
age = 23;
document.write (fName + " is " + age);
</script>

Lines 2 and 3 each declare and assign a value. On line 4, we combine the output with the plus symbol. The output is the same as shown before:

John is 23