Variables in JavaScript

Programming data have to be cached frequently. For this we use variables.

Naming

Every variable is addressed by its name. A variable name consists of letters, numbers and the underscore _. The first character mustn't be a number. A distinction is made between uppercase and lowercase. JavaScript, javascript and JAVAscript are different variable names. Examples for valid variable names are: Tool_Infy, ToolInfy, toolinfy, toolinfy1000 and _toolinfy_. Invalid variable names would be: 1toolinfy (first character is a number), tool infy (space) and tool-infy (hyphen).

JavaScript keywords mustn't be used for variable names. An example would be alert.

To assign a value to a variable you use the equal sign =. On the right side you define the value, on the left side you specify the variable name.

Numeric Variables

There are different types of variables. The american number system is used for the numeric values, which means that you use decimal points.

one = 1;
minus_twenty = 20;
one_to_five = 12345;

Strings

A string is surrounded by quotation marks - either single quotes ('') or double quotes (""). It doesn't matter whether you use single or double. It's only important that you end a string with the same type of quotes that you started it with.

website_name = "toolinfy";
html = "HyperText Markup Language";

The two quote types are useful when one of those two types is part of a string. For example you mustn't use:

text = 'That's great!';

but you can use:

text = "That's great!";

This means that you can use single inside double and the other way around. When you want to use single inside single and double inside double you have to escape the quotes that are part of the string. You can escape characters in JavaScript using the backslash \. The following examples are valid variables:

text = " \"Woooohoooo\" ";
text = 'That\'s cool!';

If a backlash should be part of a string you have to escape it with another backslash.

text = "This is one backslash: \\";

Boolean variables

Sometimes we're not interested in a number or a string. We simply want to know whether something is true or false. These values are called Boolean values.

is4anevennumber = true;
is3000aleapyear = false;

Declaring variables

As you can see, we can assign a value to a variable using an equal sign. A variable-name also can be used repeatedly and you can assign a value to a variable multiple times. In practice you often see the keyword var before the first use of the variable. This keyword is used for the initialization of a variable. First of all it helps you to see which variable has already been declared and which not. var is also important for several JavaScript functions which you will learn more about later.

Note that the keyword var can only be used once per variable. The following example is right:

var count = 1;
count = 2;

The following code would be wrong:

var count = 1;
var count = 2;

The reason is that a variable is re-initialized when var is used. This means that the old variable is deleted and a new one is created. The use of var is needless when a variable already exists.


Continue to part 5



Comment