Variables are used for storing values or expressions.
Rule 1: Variable names are case sensitive
Rule 2: Variable names must begin with a letter or the underscore character ( _ ).
Once a variable has been declared, it can be assigned a value:
myVariable = 10;
In addition, variables can be declared and assigned a value at the same time:
var myVariable = 10;
Note: Variables declared outside any function, and variables first used within functions without being declared using var, have a "global" scope.
Base-8 numbers start with a
zero and only use zero through seven.
Base-16 numbers start with 0x or
0X and use zero through nine and A through F.
num1 = 123; num2 = 0.45; num3 = 123.45; num4 = 1.23e4; // Exp. Notation num5 = 0123; // Base-8 num6 = 0xFFF; // Base-16
str1 = "A string of text"; str2 = 'A string of text'; str3 = "Text\twith\ttabs"; str4 = "John said, \"Hello\"";
bool1 = true; bool2 = 1; // also true bool3 = false; bool4 = 0; // also false
An array is a collection of data values. Each data value in an array has a number, or index. Values are retrieved from an array by enclosing an index in square brackets after the array name. Array indexes begin with zero, therefore, the first element in an array would be:
var first = myArray;
The null keyword is used to indicate that a variable has no value.
novalue1 = null;
The undefined property indicates that a variable has not been assigned a value.
var novalue1; document.write(typeof novalue1);
This produces the following result:
When the undefined value is used in a Boolean context, it converts to false. When used in a numeric context, it converts to NaN. And when used in a string context, it converts to "undefined".