CHAPTER3 Types, Values, and Variables
“Hello World.” The kinds of values that can be represented and manipulated in a
programming language are known as types, and one of the most fundamental charac-
teristics of a programming language is the set of types it supports. When a program
needs to retain a value for future use, it assigns the value to (or “stores” the value in) a
variable. A variable defines a symbolic name for a value and allows the value to be
referred to by name. The way that variables work is another fundamental characteristic
of any programming language. This chapter explains types, values, and variables in
helpful to refer to §1.1 while you read them. The sections that follow cover these topics
Boolean truth values (known as booleans). A significant portion of this chapter is dedi-
cated to a detailed explanation of the numeric (§3.1) and string (§3.2) types in Java-
Script. Booleans are covered in §3.3.
numbers, strings, or booleans. Each value is typically considered to be the sole member
of its own special type. §3.4 has more about null and undefined.
an object. An object (that is, a member of the type object) is a collection of properties
where each property has a name and a value (either a primitive value, such as a number
or string, or an object). One very special object, the global object, is covered in §3.5,
but more general and more detailed coverage of objects is in Chapter 6.
also defines a special kind of object, known as an array, that represents an ordered
working with arrays, and arrays have some special behavior that distinguishes them
from ordinary objects. Arrays are the subject of Chapter 7.
object that has executable code associated with it. A function may be invoked to run
that executable code and return a computed value. Like arrays, functions behave dif-
Functions are covered in Chapter 8.
Functions that are written to be used (with the new operator) to initialize a newly created
object are known as constructors. Each constructor defines a class of objects—the set
of objects initialized by that constructor. Classes can be thought of as subtypes of the
other useful classes. The Date class defines objects that represent dates. The RegExp
class defines objects that represent regular expressions (a powerful pattern-matching
tool described in Chapter 10). And the Error class defines objects that represent syntax
classes of objects by defining appropriate constructor functions. This is explained in
ment. This means that a program can create objects as needed, and the programmer
never needs to worry about destruction or deallocation of those objects. When an object
is no longer reachable—when a program no longer has any way to refer to it—the
interpreter knows it can never be used again and automatically reclaims the memory it
globally defined functions to operate on values of various types, the types themselves
define methods for working with values. To sort the elements of an array a, for example,
we don’t pass a to a sort() function. Instead, we invoke the sort() method of a:
a.sort(); // The object-oriented version of sort(a).
that have methods. But numbers, strings, and boolean values behave as if they had
values that methods cannot be invoked on.
be divided into types with methods and types without. They can also be categorized as
mutable and immutable types. A value of a mutable type can change. Objects and arrays
elements. Numbers, booleans, null, and undefined are immutable—it doesn’t even
make sense to talk about changing the value of a number, for example. Strings can be
thought of as arrays of characters, and you might expect them to be mutable. In Java-
Script, however, strings are immutable: you can access the text at any index of a string,
between mutable and immutable values are explored further in §3.7.
string, for example, and you give it a number, it will automatically convert the number
will convert accordingly. The rules for value conversion are explained in §3.8. Java-
Script’s liberal value conversion rules affect its definition of equality, and the == equality
operator performs type conversions as described in §3.8.1.
you can later assign a value of a different type to the same variable. Variables are
Variables declared inside a function have function scope and are visible only to code
that appears inside that function. Variable declaration and scope are covered in §3.9