Free Javascript Tutorials

home

JavaScript Tutorials

Variables

 
Our quick javascript links
Little Programmes
 
 
 
 
 
 

 

Setting up variables in JavaScript

 

You have just learnt what a variable is (Screen height and width section), and that it is a little storage area for text and numbers. Let's explore variables a bit more.

 

Variables

For our purposes, variables can hold two types of information: Numbers, and Strings. We've seen how strings work (strings are text), so let's have a go at number variables.

Number variables in JavaScript can be integers, floating point, or Boolean. An integer is a number without a decimal point; if you absolutely insist on having a decimal point then the thing you want is called a floating point number; Boolean numbers are not really numbers at all but just a record of whether something is true or not true (think of a light switch - is it on or off?). Here's some examples of all three number types:

Integer Floating Point Boolean
3 3.5 true
16 16.8 false
136 136.58 true
To store one of those number types into a variable, just use the assignment operator ( = ). The number goes on the right hand side of the assignment operator, and your variable name goes on the left. Here's an example of how to store number types into variables:

Number1 = 3
Number2 = 3.5
Number3 = true

Number1, Number2, and Number3 are our variable names. We could have used anything we liked here. As long as we're not using the handful of words JavaScript reserves for itself, and not starting with a number, and not putting any spaces in the variable name, we're all right. Here's some valid and invalid variable names

BobsNumber = 3 (valid variable name)
Bobs Number = 3 (invalid variable name - space)
3bobsnumber = 3 (invalid variable name - starts with a number)

A good idea when declaring variables, and putting values into them, is to use the word var in front of them. Like this:

var Number1 = 3

The word var is short for variable. It ensures that any variable with the same name elsewhere in your code doesn't overwrite it. Which is a pain, believe me. For clarity's sake, however, we'll leave var out of our declarations.

You can store the value in one variable name in another variable. Take this as an example:

A = 2
B = A

Here, the variable "A" is assigned a value of 2. The value inside "A" is then assigned to the variable "B". So what's inside "B"? Not the letter "A", but the number 2, of course.

If you want to add, subtract, multiply, and divide your values, you'll need the operators. The four basic operators are these :

+ (Add)
- (Subtract)
* (Multiply)
/ (Divide)

Actually, the last two should be the first two. That's because Multiply ( * ) and Divide ( / ) are done first when JavaScript is doing its sums. But, anyway, here's the operators in action. Try these scripts out in a web page, and see how you get on:

<SCRIPT Language = JavaScript>

A = 2
B = A
C = B + A
alert(C)

</SCRIPT>

<SCRIPT Language = JavaScript>

A = 2
B = 4
C = B * A
alert(C)

</SCRIPT>

<SCRIPT Language = JavaScript>

A = 2
B = 4
B = B - A
alert(B)

</SCRIPT>

<SCRIPT Language = JavaScript>

A = 2
B = 4
B = B / A
alert(B)

</SCRIPT>

<SCRIPT Language = JavaScript>

A = 2
B = 4
C = 6
D = (B * C) / A
alert(D)

</SCRIPT>

So, how did you get on? The brackets in the last script are for sectioning off code that you want handled separately. Anything in brackets gets dealt with first. So B is multiplied by C first, then the total is divided by A.

OK, let's do something a bit more useful. (Though not that much more useful!) The next part is all about adding numbers in text boxes

Move on to the Next Part -->

<--Back to the JavaScript Contents Page