We'll start with an article that describes how a computer stores values and some primitive types such as numbers and strings. You can think of the "type of a value" as defining how it is stored, named, and manipulated. For example, a calculator program uses the type "number", which holds "numeric values" and supports "arithmetic operations". JavaScript has two types: "primitive" and "object". Examples of primitive types include numbers, strings, and Booleans. Object types include arrays, objects, and functions.
Here are some things to remember when using JavaScript data types:
- Primitive types are "immutable"; their "values" cannot be changed once created;
- Object types are "mutable"; their values can change once created; and
- Although JavaScript supports types, it is a "dynamically typed" language, which means that you do not have to define the type of variable in a JavaScript program (but this is not a best practice).
Empty values
There are two special values, written null
and undefined
, that are used to denote the absence of a meaningful value. They are themselves values, but they carry no information.
Many operations in the language that don't produce a meaningful value (you'll see some later) yield undefined
simply because they have to yield some value.
The difference in meaning between undefined
and null
is an accident of JavaScript's design, and it doesn't matter most of the time. In cases where you actually have to concern yourself with these values, I recommend treating
them as mostly interchangeable.