Introduction To Javascript


Operators are used to change or gather information about variables. This page will deal only with basic arithmetical operators. Other types of operator (eg logical - and, not etc.) will appear in the guide when used in the context of a specific script.

Arithmetic with Variables

The following lines of code will add the variables a and b together and post the result in a message box.

var a = 5
var b = 10
var answer = a + b
alert("The answer is " + answer)

Exercise 7

Bearing in mind that Javascript uses the standard computer characters for arithmetic (+ - * /), create a script which will add, subtract multiply and divide the two numbers specified. Change the values of a and b a few times to see the effect.