Introduction to DataTypes and Values
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).
Automatic type conversion
In the Introduction, I mentioned that JavaScript goes out of its way to accept almost any program you give it, even programs that do odd things. This is nicely demonstrated by the following expressions:
console.log(8 * null) // → 0 console.log("5" - 1) // → 4 console.log("5" + 1) // → 51 console.log("five" * 2) // → NaN console.log(false == 0) // → true
When an operator is applied to the "wrong" type of value, JavaScript will quietly convert that value to the type it needs, using a set of rules that often aren't what you want or expect. This is called type coercion. The null
in the first expression becomes 0
, and the "5"
in the second expression becomes 5
(from string to number). Yet in the third expression, +
tries string concatenation before numeric addition, so the 1
is converted to "1"
(from number to string).
When something that doesn't map to a number in an obvious way (such as "five"
or undefined
) is converted to a number, you get the value NaN
. Further arithmetic operations on
NaN
keep producing NaN
, so if you find yourself getting one of those in an unexpected place, look for accidental type conversions.
When comparing values of the same type using ==
, the outcome is easy to predict: you should get true when both values are the same, except in the case of NaN
. But when the types differ,
JavaScript uses a complicated and confusing set of rules to determine what to do. In most cases, it just tries to convert one of the values to the other value's type. However, when null
or undefined
occurs on either side of the operator, it produces true only if both sides are one of null
or undefined
.
console.log(null == undefined); // → true console.log(null == 0); // → false
That behavior is often useful. When you want to test whether a value has a real value instead of null
or undefined
, you can compare it to null
with the
==
(or !=
) operator.
But what if you want to test whether something refers to the precise value false
? Expressions like 0 == false
and "" == false
are also true because of automatic
type conversion. When you do not want any type conversions to happen, there are two additional operators: ===
and
!==
. The first tests whether a value is precisely equal to the other, and the second tests whether it is not precisely equal. So "" === false
is false as expected.
I recommend using the three-character comparison operators defensively to prevent unexpected type conversions from tripping you up. But when you're certain the types on both sides will be the same, there is no problem with using the shorter operators.