JavaScript Variables and Operators

Defining Variables

The best way to understand variables better is to actually create some.

To create a local variable we use the reserved word "var" followed by the name of the variable we are creating.

var apples;

This statement has defined a local variable and given it the variable name apples. Note that the word var must be in all lowercase and the variable that we have just created is a different variable from Apples or APPLES.

At this point you are probably wondering just what value we have currently stored in this variable. Currently, the value of apples is undefined. This is a special value that JavaScript assigns to any variable that is created where an initial value is not specified.

Note: undefined is the simplest type of data that JavaScript can work with because there is only one value that this data type can have. The only value that a variable of type undefined can contain is undefined.

If we actually want to assign an initial value to a variable, we need to specify the value that we want to assign. Here are a few examples of how to do this.

1 var male = true;
2 var bedrooms = 3;
3 var myName = "Stephen";

Note that in JavaScript as in most programming language the = symbol doesn't mean that the entries on both sides are equal to one another. What it means is that the value on the left is to be made equal to the value on the right.

Each of these examples creates a variable that holds a value with a different type.

1 creates a Boolean variable named male. A Boolean variable can contain one of two values – true and false. Boolean variables are extremely useful when it comes to making comparisons between two values since the result of the comparison will be a Boolean variable.

2 creates a variable that contains a number – in this case the initial value of that number is 3.

Note that in each of these first two examples the value being assigned is not enclosed in either quotes or apostrophes. The names true and false are reserved words that specifically identify the two possible values that a Boolean variable can hold. Any value that starts with a number is considered to be a numerical constant rather than a variable (this is how JavaScript tells what parts of the code are variable names and what are not). Numerical constants are numbers that are “hard-coded” into your script. When creating a numerical constant there are only twelve characters that you are allowed to use in specifying the number. Ten of these are the obvious ones as they are the numbers 1234567890 and the eleventh is almost as obvious – the period character that represents the decimal point in numbers that are not integers. The twelfth and not very obvious character that can be used in numbers it the letter e. This is used to specify numbers in scientific notation. The number 1e8 and 100000000 are the exact same number written in two different ways .

Any character or characters enclosed within quotes or apostrophes is considered to be a text string. In 3 "Stephen" is a text string. You can include just about any character at all inside text strings in JavaScript. Here are some more examples of text strings.

1 'Australia'
2 "true"
3 "15"
4 '"Beware" he said'
5 "'"
6 ' He\'s "zonked"'

1 illustrates the use of apostrophes (single quotes) to define the start and finish of a text string instead of quotes. In JavaScript both are valid.

2 and 3 show how if we include a reserved word such as true or a number inside quotes or apostrophes then we have a text string rather than a Boolean or a number. They have the same apparent value but a different data type. In fact “false” evaluates to the Boolean value true.

4 shows a text string contained within apostrophes that contains quotes within the content of the string itself 5 shows the reverse of this, a text string that is contained within quotes and consists of a single apostrophe. The easiest way of including either quotes or apostrophes in a text string is to use the other one as the string delimiter (to mark where the text starts and finishes).

6 shows how we can include both apostrophes and quotes in the same text string. Because there are more quotes than apostrophes in the actual text we delimit the text with apostrophes. This then just leaves us with being able to tell JavaScript that the second apostrophe is a part of the string rather than indicating the end of the string. We do this by placing a \ character (known as a backslash or slosh) in front of the apostrophe in order to “escape” its meaning of end of text marker and so allow it to be a part of the text. The \ is also called an escape character because it escapes the regular meaning of the character following it rather than being part of the text itself.

Finally, 7 shows how we escape the escape character so as to restore its meaning as a backslash character. This also illustrates a common trap that people fall into when coding Windows style path references into their script as they forget to escape the backslash characters.

go to previous section go to next section 


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow