CoP004 Javascript
CoP004 Javascript
Variables
Variables are a way of storing information. The variables x and y are often used in algebra, assigning a value to each e.g. x = 2 and y =
4. We can also use variables in JavaScript, but you must declare them first using the term ‘var’. Declaring a variable is a way of saying
“a variable exists and this is its name” e.g.
var valueX = 2
var valueY = 4
var valueZ
Task 1
Using the fact that valueX = 2 and valueY = 4, write down the value of the variable ‘valueZ’ in each case below:
a. valueZ = valueX + 3
b. valueZ = valueX * 2 (* is the symbol used for multiply)
c. valueZ = valueX / 2 (* is the symbol used for divide)
d. valueZ = valueX + valueY
e. valueZ = (valueX – valueY) * 7
Task 2
Add the following code to a new page and look at it in ‘Preview’ (remember that the JavaScript is placed between the </title> and
</head> tags.
<script LANGUAGE="JavaScript">
<!-- A semi-colon (;) is used at the end of
var valueX = 2; each statement
var valueY = 4;
var valueZ = valueX + valueY;
alert(valueZ);
//-->
</script>
Task 3
Adjust the script so that it calculates and displays each of the following sums (there are many ways of achieving the correct results).
Write down the answers.
b. 25 * 378 __________________
c. 3578 / 72 __________________