JavaScript is a client-side scripting language for web pages that supports objects. With its ability to manipulate the DOM JavaScript is transformed into a powerful scripting language that makes web pages dynamic.
JavaScript statements
A statement is a section of code that performs a single action
eg: hours = now.getHours ();
A semi-colon indicates the end of a statement.
JavaScript functions
document.write(“Testing.”);
the above is a statement with a section in parentheses. The section is an example of a function. So as not to repeat statements you can create your own functions so that they may be called multiple times.
Variables
Variables are containers that can contain a number, string of text, or another value. For example, the following statement creates a variable called fred and assigns it the value 27:
var fred = 27;
Conditionals
if (count ==1) alert(“The countdown has reached 1.”);
Loops
for (i=1; i<=10; i++) {
Alert(“Yes, its yet another alert!”);
}
Event Handlers
Not all scripts are located within the <script> tags. You can also use scripts as event handlers. Below is a typical HTML image tag with an event handler.
<img src=”button.gif” onMouseOver=”highlight();”>
Syntax Rules
Case sensitivity
- Javascript keywords such as if and for, are always lowercase
- Built in objects such as Math and Date are always capitalized.
- DOM object names are usually lowercase but their methods are often a combination of capitals and lowecase. Usually capitals are used for all but the first word, as in toLowerCase and getElementById.
Variable, Object and Function names
Names must begin with a letter or underscore but may contain numbers upercase and lowercase letters and the underscore character. Names must also not be reserved words that make up the javaScript language such as if, for and DOM object names such as window and document.
Spacing
Blank space (known as whitespace by programmers) is ignored by Javascript. Space often makes the script more readable.
Comments
single line: //this is a comment
Multiple lines:
/*this is
a comment*/
Objects
Like variables, objects can store data, but they can store one or more pieces of data at once. The items of data stored in an object are called properties of the object. For example, you may could use objects to store information about people such as in an address book. The properties of each person object might include a name, an address and a telephone number.
JavaScript uses periods to separate object names and property names. For example Bob.address and Bob.phone.
Objects can also include methods. These are functions that work with the objects data. For example, our person object might include a display() method.
In JavaScript that statement would be Bob.display()
Expressions and Operators
An Expression is a combination of variables and values that the JavaScript interpreter can evaluate to a single value. The characters that are used to combine these values are called operators.
Data Types in Javascript
In JavaScript you dont need to specify a data type in most cases. These are the basic JavaScript data types:
- Numbers such as 5, 34 and 2.445. JavasScript supports both integers and floating point numbers
- Boolean, or logical values. These can have one of two values: true or false.
- Strings such as “This is a test.”. Stictly speaking these are String objects.
- The null value, represented by the keyword null. This is the value of an undefined variable.
Variables can have their data type changed midstream. Although this is convenient and powerful it can also lead to errors and miscalculation.
Arrays
An array is a numbered group of data items that you can treat as a single unit. For example you might use an array called scores to store several scores for a game. Arrays can contain strings, numbers, objects or other types of data. Each item in an array is called an element of the array.
Numeric Arrays
Unlike most other types of JavaScript variable you typically need to declare an array before using it. The following creates an array with four elements.
Scores= new Array(4);
to assign a value to an array you use an index in brackets. Indexes begin with 0, so the elements in the array in this example would be numbered 0 to 3.
scores[0] = 39;
scores[1] = 1;
scores[2] = 100;
scores[3] = 33;
You can also declare an array and assign values at the same time:
scores = new array (39,1,100,33);
In JavasScript 1.2 and later you can also use a shorthand syntax:
scores = (39,1,100,33);
String Arrays
Javascript also enables you to use arrays of strings. This is a powerful feature that enables you to work with large numbers of strings at the same time.