The JavaScript Workshop
上QQ阅读APP看书,第一时间看更新

Fixed Types

Fixed types are types that have no variation of value. Unlike numbers, which may have any combination of digits, an optional minus sign (for negative numbers), a decimal point, or a scientific notation, a fixed type is always one simple value or value group.

In JavaScript, the available fixed types include null, undefined, and Booleans (true and false). These values are static and cannot change. If a variable contains one of these values, it is then strictly equal to the value itself. Fixed types are more of a representation of a situation than actual data. For instance, true is a fixed representation of truthfulness and false is a fixed representation of falsehood. The values are not quantifiable in the real world but are representative of logic that software directly deals with.

The null Value

null, in mathematical terms, denotes a value that is not present. In JavaScript, null is a static value that's used to mean no value. In other languages, this would be equivalent to nil or void.

null is a useful value for dereferencing variables or for returning a value from a function when no value can be returned. For instance, a function may return an object from an array if the item is present but may return null if it is not.

The undefined Value

undefined is similar to null in many ways and, due to this, the two values are often misused. undefined is the value contained in any variable that is declared first, but not assigned a value. It is also the value that's been returned from a function that does not explicitly return a value using the return keyword and it is the value that's returned from a statement (an action with no resulting value).

When working with undefined, you should always anticipate it, but never assign it to a variable or return it explicitly from a function. In such circumstances, you should use null.

Boolean Values

Boolean is a term named after George Boole, a nineteenth-century English mathematician and philosopher. It is used to denote the values true and false. These values may be assigned to variables and are strictly equivalent to their value, like null.

Booleans are unique among the types supported by JavaScript because they are indirectly comparable to other types and expressions. The logical operators described in Chapter 3, Programming Fundamentals, of this book, for instance, all result in a Boolean value.