Sometimes a statement will make use of, or call, a function which is code stored somewhere else in the program or in the browser to carry out some complex function that can be represented in the statement by a single function name. To convert a string value to a number, for example, a statement my use parseInt(word) which will call the parseInt() function and replace it in the statement with the numeric equivalent of the value represented by the variable, word.
An expression is just a formula that converts to some value. An operator is something that produces an effect on one or more variables. In the following example, “Hello “ is a string constant and yourName is a variable. The two are related by a + operator which has the effect of concatenating them into a single string.
“Hello “ + yourName
If we now add another operator, the assignment operator, =, and another variable, called message, we can have a complete statement that actually does something, as follows:-
message = “Hello “ + yourName;
This statement assigns the value of the expression on the right to the variable on the left and is called an assignment statement.
A mathematical assignment statement is similar to a mathematical formula. For example,
total = x + y + z;
And as an example of a function call, we might have
number = parseFloat(userInput) * 100;
This converts a user input to a floating point number, multiples it by 100 then assigns the value of this expression to the variable number.
An assignment statement uses an = operator to assign the value of some expression to a variable. We usually say “equals” but it does not mean “is equal to”. We have another operator for that. It means “is to be assigned a value equal to”. For example,
label.x = 20; label.x is assigned the value 20, its x-coordinate.
label.y = 30; label.y is assigned the value 30, its y-coordinate.
label.text = “Next”; label.text is assigned the string value “Next”.
or, reusing an earlier example,
String(text) = 5 + (13 – 3) * 3 / 2
which would evaluate the mathematical expression on the right, and assign the result to the string value of a variable, text, after casting it as a String variable.
An important result of this interpretation of assignment is that it can involve a variable being assigned to itself. For example, the assignment,
x = x;
is quite acceptable and would compile all right – although it would not have any effect. It would just assign the value of x to its current value. However, the statement
x = x + 1;
would certainly have an effect and it is very widely used in programming to increment the value of a variable. It means that the value of x should be assigned its current value plus 1.
Now we can look at some of these concepts in more detail.
A variable is an entity that represents a piece of data in a program. It is so called because its value may change during the execution of a program. Like the variables used in mathematics, it is usually a symbol, often a character or word, that represents some value. It is a placeholder for data to be supplied to the program or, in the maths case, the formula.
For example a, b and c may be variables that represent three numbers stored by the program. Their values may be input by a user or obtained by calculation and they may change during program execution. Without variables, there would be no way to write a program that can be used with any user or other input. Variables are what make programs programmable in advance of their use. Without them, data would have to be stored entirely in the program, such as in an ebook.
As you might expect, all variables must have unique names, or identifiers, that provide a means of identifying them. For example, x might be a variable representing the x position of some object and y its y position. As another example, a program variable, yourName might be used to get the name of the user then the rest of the program can use the value of yourName without knowing what it might turn out to be at runtime. It should work for all values of yourName, even unexpected ones. Testing a program usually involves trying outlandish values of input to variables to see how the program behaves.
It is good practice to declare a variable before it is required in a program, usually somewhere near the beginning of the program. This ensures that all the variable names are collected together and helps to avoid duplication and name collision. They are usually declared with the var keyword. This means that variables declared inside functions are only visible in these functions and further helps to avoid collisions with variables declared in other functions. Without the var keyword they are global variables and visible everywhere.
There are four built-in types, apart from objects, as follows:-
Number any kind of number – floating point decimal or integer
String a string of text characters, or a single character
Boolean a Boolean value of true or false
Array an array of other variables
They are declared, for example, as follows:-
var num; to declare a variable named num probably to hold a Number value.
var word; to declare a variable named word probably to hold a String value.
Variables, at some stage in a program, have to be given values. If this is done when they are declared, it is described as initialization. It is a good idea to initialize variable since they may be used in a program before they have been given values and produce unpredicted results. For example we might have,
var num = 45;
var word = “next”;
Note the use of semicolons to terminate each of these declarations and initializations.
Note also that the value of a string is always stated inside quotation marks. These can be double quotes, as above, or single quotes. And, of course, we can have strings that look like numbers, such as “2011”. These are typically used for telephone numbers and addresses that are not going to be processed as numbers.
And one other oddity needs to be mentioned. We can have String values with nothing between the quotes, for example “”. This is called a null string and is used to make sure memory space is allocated without giving the string a value.
The attributes of HTML tags also behave like variables. The tags themselves can be regarded as objects of the tag type, their id values are their names and all other attributes are their properties represented by variables which are the attribute names. For example the following code declares an object of type img, with a name of slide1 and an attribute called src which specifies its location as well as width and height variables..
<img id=”slide1” src=”slide1.jpg” width=”200” height=”150”>
So the whole page is an application with objects and variables that can be used by any embedded scripts.
//This is a comment
/* This is a comment */
The only unary operator required here is the minus sign, -, which converts the value of a number to its negative equivalent. So -5 would have the usual value.
Arithmetic operators are the familiar set of * (multiply by), / (divide by), + (add), - (subtract) and brackets, ( and ). These are used exactly as they are used in mathematical expressions evaluating them in the order of bracket contents first, * and / next (from the left if there is any ambiguity, then the + and – operators (again from the left, if necessary). This is described as operator precedence. So, for example the following arithmetic expression evaluates to 20.
5 + (13 - 3) * 3 / 2 = 5 + 10 * 3 / 2 = 5 + 30 /2 = 5 + 15 = 20
Note that the * operator is never omitted next to brackets as it is in mathematics. For example, we must write 7*(6–2) rather than 7(6-2).
There are rules of precedence for all operators – not just the arithmetic ones.