If you have no programming experience then you may be unfamiliar with the concept of variables. A variable is a value, stored in the computer's memory, which can be varied.
To be able to read a variable's value or alter its value we don't need to know where it's stored in memory - JavaScript takes care of that - we just need to know its name.
Variable Names
There are naming conventions that must be followed. Variable names ...
Cannot be any of the keywords used in JavaScript e.g. var, function, break, true, false etc.
Are case sensitive e.g. My_Variable and my_variable are different variables.
Variable names are usually chosen to reflect their use and if two words make things clearer then underscores or capitalization is normally used e.g. customer_count or CustomerCount.
Data Types
JavaScript supports three primitive data types and two composite data types.
A variable holding a primitive type only holds one value at a time whereas a variable holding a composite type can hold more than one value (composite data types are covered in a later tutorial).
Privitive Data Types
Boolean - that can only hold one of two values either true or false e.g. test = true.
Floating-point - that can hold floating point or integer numbers (JavaScript represent all numbers as floating-point).
String - that can hold a string of characters e.g. name = "Dave".
Using Variables
Unlike some languages, JavaScript is untyped. This means that it's possible to assign a numeric value to a variable then later assign a string.
It also means that the language will automatically convert a value from one type to another, where required.
Variable Declaration
Before a variable can be used in a program it should be declared using the var keyword. Multiple variables may be declared in one statement separated by commas.
Examples of Variable Declarations
<script language="javascript" type="text/javascript">
var count;
var firstname, lastname;
</script>
Variable Initialization
A value is assigned to a variable using the = sign and initialization may be combined with declaration.
Until a a value is assigned to a variable then its value is undefinded.
Examples of Variable Initialization
<script language="javascript" type="text/javascript">
var count;
var firstname = "Dave";
var lastname = "Clark";
count = 10;
</script>
Operating on Variables
It's common to want to add or subtract 1 to or from a variable (increment or decrement) and this can be achieved in different ways.
Examples of Increment and Decrement
<script language="javascript" type="text/javascript">
var count = 10;
// Different ways to increment count
count = count + 1;
count++;
// Different ways to decrement count
count = count - 1;
count--;
</script>
The way increment and decrement behave depends on whether they appear before or after the operand.
If before then it's known as pre-increment or pre-decrement - if after then it's known as post-increment or post-decrement.
Pre-increment or pre-decrement increments or decrements the operand and evaluates to the incremented or decremented value of the operand.
Post increment or post-decrement increments or decrements the operand but evaluates to the unincremented or undecremented value of the operand.
The example below illustrates the difference.
Examples of Post-Increment and Pre-Increment
<script language="javascript" type="text/javascript">
// Example of post-increment
var count = 10;
total = count++;
// After this count = 11 and total = 10
// Example of pre-increment
var count = 10;
total = ++count;
// After this count = 11 and total = 11
</script>
String Variables
A string variable consists of zero or more characters
e.g. my_string = "Hello there";.
Strings can be added together (concatenated) using the + character.
Examples of Concatenation
<script language="javascript" type="text/javascript">
my_string = "The beginning" + " and the end";