Javascript Lesson Zero

To see the JavaScript program code you will need to click on "view" and then "source". JavaScript code is usually found in the head part of an HTML document between "script" and "/script" tags.

JavaScript and HTML are designed to be able to interact with each other. The function which I have called "clicked" is written in JavaScript, but the button code is written in HTML: onclick="clicked()" ties the two systems together and enables the HTML code to trigger the JavaScript code into action.

The JavaScript instruction "var" is used to set up one or more variables, a variable being a piece of memory set aside to hold data. In this case the variables are initially only given names, apart from the last one "f" which is given a value to hold.

Functions are always set out in the same way, the word "function" being followed by a name and pair of brackets (often empty, but they can sometimes be used to pass variables to the function), and then the code used to perform the function has to be placed between a pair of curly brackets {}. The instruction "alert" is usually used to help you debug a program while you are writing it, but here I have used it here to show you what the program is doing while it runs.

Notice that JavaScript uses * for multiplying and / for dividing, rather than the usual mathematical symbols: this happens in most programming languages. Notice also how f is given a new value by the f=e/f bit of code: f was given the value 3 earlier, but now it is given a new value calculated by dividing e by the old value of f (or rather, what is about to become its old value). Study the code carfully and run it repeatedly until you understand how it does what it does.