Primitive Data-types and Constants
Before discussing data-types it might be useful to ask the question, "What
is data?"
Data is: ...
In JavaScript these are the "primitive" (i.e. the most basic) data types:
- Numbers: 5, -10, 10.5, 5e-15
- Boolean: true & false
- Boolean values and expressions are either true or false
and can be nothing else.
- Strings: "Hello, world!", 'abc'
- Composed of characters enclosed by single or double quotes.
- Arrays: [1, 2, "abc", true]
- A list of data items. If a data item was a piece of paper, an array
is like a folder holding the papers, or a book.
- Each item in the array has an index location (like the papers location
in the folder or stack of papers or page number in a book.) This index
starts at 0 and increases by one for each item.
- Every element in an array can be any data type as well.
- Objects: { name:"value", foo: 1, bar: [1,2] }
- To talk about later, but they are a lot like arrays, but we can use
a name to refer to each data item rather than an index number.
- Like arrays, every value associated with a name can be a different
data-type.
- Bottom types: undefined / null
- They have no real intrinsic value, but indicate something about a
variable, namely that it contains no value.
- A variable that has not been defined or given a value or a location in
an array that has not been given a value is undefined.
- null is mostly used to clear a variable of a pre-existing value
or used to indicate that something that might normally point to an object
is not currently doing so.
- Either might be used to indicate an error condition when used as the
return value from a function. When used in Boolean expressions both are
considered false.
Data values like 10, "test", true, etc, are called
"literals" or "constants", because they cannot be modified,
they are literally that value.