Variables allow you to refer to stored data by name, and manipulate the data.
How to declare a variable
myName = "Steve";
If you use a variable in this way inside of a function, it will be globally available to the rest of the script, for example:
MyName = "Steve";
To make a variable local to a function, use the keyword var in front of the variable. Modify the code above to add var in front of MyName:
var MyName = "Steve";
Because the variable MyName is now local to the setMyName() function, you can only use it within the function. Using var also helps the interpreter free the memory of the variables that are no longer needed.
MyName = "Steve"; // The value is stored in a variable of string type
MyAge = 23; // The value is stored in a variable of numeric type
BornInAustralia = true; // boolean type
Rules for naming variables
The first character must be a letter, or an underscore (_), or a dollar sign ($).
The subsequent characters can be letters, numbers, underscores, or dollar signs.
The variable must not contain any embedded space characters.
The variable name can't be a reserved word.
myName // Valid variable
_indexNumber // Valid variable
6item // Invalid: starts with a number
Trying to use a variable that has not been declared generates an error. When you declare a variable without assigning it a value, it is undefined. You can assign a special value null to a variable.
myNumber = null;
price = 20;
str ="This book costs " + price + " dollars."
Convert a string to a number is less straight-forward. Consider the following example:
price = "20";
price = price + 10;
In cases like this, it helps to use the explicit conversion functions like parseInt() and parseFloat():
price = "20";
price = parseInt(price) + 10;
Strings can have both single or double quotation marks, for example:
str = "This is a string";
str = 'This is a string';
str = ' "Do not mess up with my strings," he excalimed.'
Number Data Type
NaN, or not a Number
Integers can be represented in base 10 (decimal), base 8 (octal), and base 16 (hexadecimal).
Octal integers are specified by a leading "0", and can contain digits 0 through 7. If a number has a leading "0" but contains the digits "8" and/or "9", it is a decimal number. A number that would otherwise be an octal number but contains the letter "e" (or "E") generates an error.
Hexadecimal ("hex") integers are specified by a leading "0x" (the "X" can be uppercase or lowercase) and can contain digits 0 through 9 and letters A through F (either uppercase or lowercase). The letter "e" is a permissible digit in hexadecimal notation and does not signify an exponential number. The letters A through F are used to represent, as single digits, the numbers that are 10 through 15 in base 10. That is, 0xF is equivalent to 15, and 0x10 is equivalent to 16.
Octal and hexadecimal numbers can be negative, but cannot be fractional. A number that begins with a single "0" and contains a decimal point is a decimal floating-point number; if a number that begins with "0x" or "00" contains a decimal point, anything to the right of the decimal point is ignored.
// Four floating-point numbers, equivalent to each other.
.0001, 0.0001, 1e-4, 1.0e-4
// A floating-point number, equivalent to 345.
// An integer number.
// An octal integer, equivalent to 255.
// As octal numbers cannot have decimal parts, this is equivalent to 0.
// An integer, equivalent to 378.
// A hexadecimal integer, equivalent to 255.
// A hexadecimal integer, equivalent to 14287.
// A hexadecimal integer, equivalent to 999.
// As hexadecimal numbers cannot have decimal parts, this is equivalent to 3.
The possible Boolean values are true and false. You cannot use a Boolean type instead of numbers 1 and 0.
Undefined Data Type
A value that is undefined is simply a value given to a variable after it has been created, but before a value has been assigned to it.
Null Data Type
A null value is one that has no value and means nothing.