A variable is a unit of data storage that we define, whose value can be set and modified. We use variables as a way to contain and retrieve values that we need to work with, using the names we give them. Variables are stored only in memory, and not actually written to disk.
Variables in JavaScript can take values in any of six data types. The main types are strings, numbers, objects, and Booleans. The other two are null and undefined.
This relatively small set of data types enable us to perform a broad range of useful functions with our applications.
Strings are character data, which can be a letter, a word, a phrase or a sentence. Also, numbers not used for math operations can be treated as strings, such as a zip code, for example.
Strings must be delineated by single or double quotation marks. We use single quotes to type strings that contain double quotes within the string.
A string is also an object in JavaScript, but it is a special case, with special properties. The following are examples of strings:
"This is a string."
'And again the boy yelled, "wolf, wolf!"'
"123 ABC Street"
A string can contain zero or more unicode characters. When it does not contain anything, it is called a zero-length string ( "" ).
JavaScript supports both integer and floating-point numbers. Integers can be positive, 0, or negative; a floating-point number can contain either a decimal point, an "e" ( uppercase or lowercase ), which is used to represent "ten to the power of" in scientific notation, or both. These numbers follow the IEEE 754 standard for numerical representation.
The following are examples of valid numerical values.
123 // An integer or whole number.
3.45e2 // A floating-point number, equivalent to 345.
.0001, 0.0001, 1e-4, 1.0E-4
// Four floating-point numbers, equal to each other.
Numeric values are explored in detail in Numbers in JavaScript.
The only possible Boolean values are true and false. These are special values, and are not usable as 1 and 0.
Note that in a comparison, any expression that evaluates to 0 is taken to be false, and any statement that evaluates to a number other than 0 is taken to be true. Thus the following expression evaluates to true:
( false == 0 )
For more information on comparisons, see Controlling Program Flow.
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.
A null value is one that has no value and means nothing.