Part 1

JavaScript

 

History

JavaScript was designed to 'plug a gap' in the techniques available for creating web-pages.

HTML is relatively easy to learn, but it is static. It allows the use of links to load new pages, images, sounds, etc., but it provides very little support for any other type of interactivity.

To create dynamic material it was necessary to use either:

 

Netscape Corporation set out to develop a language that:

They came up with LiveScript.

Netscape subsequently teamed-up with Sun Microsystems (the company that developed Java) and produced JavaScript.

Javascript only runs on Netscape browsers (e.g., Netscape Navigator). However, Microsoft soon developed a version of JavaScript for their Internet Explorer browser. It is called JScript. The two languages are almost identical, although there are some minor differences.

 

Internet browsers such as Internet Explorer and Netscape Navigator provide a range of features that can be controlled using a suitable program. For example, windows can be opened and closed, items can be moved around the page, colours can be changed, information can be read or modified, etc..

However, in order to do this you need to know what items the browser contains, what operations can be carried out on each item, and the format of the necessary commands.

Therefore, in order to program internet browsers, you need to know:

In this course we will be using JavaScript/JScript to program browsers. However, there are several other languages we could use should we wish to. Therefore, we shall try to distinguish clearly between those aspects of internet programming which are specific to JavaScript/JScript and those which remain the same regardles of which language we choose to use.

We'll start by looking at some of the basic features of the JavaScript language.

 

Variables & Literals

A variable is a container which has a name. We use variables to hold information that may change from one moment to the next while a program is running.

For example, a shopping website might use a variable called total to hold the total cost of the goods the customer has selected. The amount stored in this variable may change as the customer adds more goods or discards earlier choices, but the name total stays the same. Therefore we can find out the current total cost at any time by asking the program to tell us what is currently stored in total.

A literal, by contrast, doesn't have a name - it only has a value.

For example, we might use a literal to store the VAT rate, since this doesn't change very often. The literal would have a value of (e.g.) 0.21. We could then obtain the final cost to the customer in the following way:

VAT is equal to total x 0.21

final total is equal to total + VAT

 

JavaScript accepts the following types of variables:

Numeric Any numeric value, whether a whole number (an integer) or a number that includes a fractional part (a real), e.g.,
  12
3.14159
etc.
 
String A group of text characters, e.g.,
  Ian
Macintosh G4
etc.
 
Boolean A value which can only be either True or False, e.g.
  completed
married
etc.

 

We create variables and assign values to them in the following way:

var christianName = "Fred" (string)
var surname = "Jones" (string)
var age = 37 (numeric)
var married = false (Boolean)

Note that:

 

Operators

Operators are a type of command. They perform operations on variables and/or literals and produce a result.

JavaScript understands the following operators:

+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus

(If you're not sure what a modulus operator does, here are some notes and an example)

These are known as binary operators because they require two values as input, i.e.:

4 + 3

7 / 2

15 % 4

 

In addition, JavaScript understands the following operators:

+ + Increment Increase value by 1
 
- - Decrement Decrease value by 1
 
- Negation Convert positive to negative, or vice versa

These are known as unary operators because they require only one value as input, i.e.:

4++ increase 4 by 1 so it becomes 5
7-- decrease 7 by 1 so it becomes 6
-5 negate 5 so it becomes -5

 

JavaScript operators are used in the following way:

var totalStudents = 60
var examPasses = 56
var resits = totalStudents - examPasses

Note that by carrying out this operation we have created a new variable - resits.

There is no need to declare this variable in advance.

It will be a numeric value because it has been created as a result of an operation performed on two numeric values.

 

We can also combine these operators (and the assignment operator, =) in certain ways.

For example:

  total += price

performs the same task as:

  total = total + price

Similarly,

  total *= quantity

performs the same task as:

  total = total * quantity

 

Below is a full list of these 'combined' operators.

+ = 'becomes equal to itself plus'
- = 'becomes equal to itself minus'
* = 'becomes equal to itself multiplied by'
/ = 'becomes equal to itself divided by'
% = 'becomes equal to the amount which is left when it is divided by'

You may find the descriptions helpful when trying to remember what each operator does. For example:

5 * = 3

can be thought of as meaning:

5 becomes equal to itself multiplied by 3