ASP Net Center.com
JavaScript
Introduction
Variables & Arrays
Functions
If Statements
Case Statements
Loop Statements
Alert
Form Processing
Date Objects
Window Objects

Variables

Variables are used to store information. A variable's value can change during the script. You could have someone's last name and first name stored in the word name. JavaScript variables are case sensitive so watch upper case and lower case letters when referring to a variable. They also must start with letter or underscore.

Declare a variable in JavaScript variable like this:
var name;. This means, create variable name.
Var is term used to declare a java and JavaScript variables.
Assign a value to the variable like this:
name="G. Danyeer";. This simply assigns the value G. Danyeer to the variable name. You can declare and assign a value in single statement as follows:
var name = "G. Danyeer";

Arrays

An array is an indexed list of things called elements or group of related variables.   Element value can be whatever you want them to be such as numbers or strings.  Array is helpful when ever you want to keep track of a group of related items. Say that we want declare variables for list of cars like this;
var car1="Camry";
car2="Corolla";
car3="Accord";
car4="Civic";
car5="Maxima";

The list can go on and on. Here is an array way to do the same.
var cars=new Array()
car[0]="Camry";
car[1]="Corolla";
car[2]="Accord";
car[3]="Civic";
car[4] ="Maxima";

Note that the first element of array is 0. The variable car now contains list of cars indexed by numbers 0 to 4. What about if we want access the list? We can access the list all at once using loops or individually. Here is how you would write the list on the browser all at once.
var x=0;
for(x=0;x<4;x++);
{
cars[x];
document.write(cars[x]);
}

We declared variable x with initial value of zerro. It's important to initialize variables in some languages such as c++ but not important for JavaScrip. Then we have loop statement for (x=0;x<4;x++).  This loop increments x 0 to 4 and the variable x is place for each index number by assigning x into cars[x]. The document.write statement, writes the value each time that new element is read.

The following is a sample of the array explained above.
<html>
<body>
<script language="javascript">
{
    var cars = new Array()
    cars[0]="Camry";
    cars[1]="Corolla";
    cars[2]="Accord";
    cars[3]="Civic";
    cars[4]="Maxima";
    for(x=0;x<=4; x++)
   {
            cars[x];
        document.write(cars[x]+"<br>");
   }
}
</script>
</body>
</html>

Copy this code and place it in any notepad, textpad, or any other text editor program.
  • Save it as "fileName.html"
  • Open the internet browser
  • Go file, open, browse
  • Select fileName.html you just saved
  • Click open
The result will look like as follows:
Camry
Corolla
Accord
Civic
Maxima

We can display the result on message box, window or where ever we want.  Say that we want display the result on message box when we click on a button.  Here is what we would change.  Change cars[x] to alert(cars[x]} then remove the java script code from the body and replace with this statement.  Put the removed code in the head section within a function called listOfCars(). <input type="button" value="Display the car inventory" name "me" onclick="listOfCars();">
The result would look like this:

Introduction to JS Functions