JavaScript – Types, Values, and Variables

Computer programs work by manipulating
values, such as the number 3.14 or the text
“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
characteristics 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
JavaScript. These introductory paragraphs provide an overview, and you
may find it helpful to refer to Core JavaScript while
you read them. The sections that follow cover these topics in
depth.

JavaScript types can be divided into two categories:
primitive types and object
types
. JavaScript’s primitive types include numbers, strings
of text (known as strings), and Boolean truth
values (known as booleans). A significant portion
of this chapter is dedicated to a detailed explanation of the numeric
(Numbers) and string (Text) types
in JavaScript. Booleans are covered in Boolean Values.

The special JavaScript values null and undefined are primitive values, but they are
not numbers, strings, or booleans. Each value is typically considered to
be the sole member of its own special type. null and undefined has more about null and undefined.

Any JavaScript value that is not a number, a string, a boolean, or
null or undefined is 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 The Global Object, but more
general and more detailed coverage of objects is in Chapter 6.

An ordinary JavaScript object is an unordered collection of named
values. The language also defines a special kind of object, known as an
array, that represents an ordered collection of
numbered values. The JavaScript language includes special syntax for
working with arrays, and arrays have some special behavior that
distinguishes them from ordinary objects. Arrays are the subject of
Chapter 7.

JavaScript defines another special kind of object, known as a
function. A function is an 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 differently from other
kinds of objects, and JavaScript defines a special language syntax for
working with them. The most important thing about functions in
JavaScript is that they are true values and that JavaScript programs can
treat them like regular objects. 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 object type. In addition to the Array and Function
classes, core JavaScript defines three 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 and runtime errors that can occur
in a JavaScript program. You can define your own classes of objects by
defining appropriate constructor functions. This is explained in Chapter 9.

The JavaScript interpreter performs automatic garbage
collection
for memory management. 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 was occupying.

JavaScript is an object-oriented language. Loosely, this means
that rather than having 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).

Method definition is covered in Chapter 9.
Technically, it is only JavaScript objects that have methods. But
numbers, strings, and boolean values behave as if they had methods
(Wrapper Objects explains how this works). In JavaScript,
null and undefined are the only values that methods
cannot be invoked on.

JavaScript’s types can be divided into primitive types and object
types. And they can 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 are mutable: a JavaScript program can
change the values of object properties and array 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 JavaScript, however, strings are immutable: you can
access the text at any index of a string, but JavaScript provides no way
to alter the text of an existing string. The differences between mutable
and immutable values are explored further in Immutable Primitive Values and Mutable Object
References.

JavaScript converts values liberally from one type to another. If
a program expects a string, for example, and you give it a number, it
will automatically convert the number to a string for you. If you use a
nonboolean value where a boolean is expected, JavaScript will convert
accordingly. The rules for value conversion are explained in Type Conversions. JavaScript’s liberal value conversion rules
affect its definition of equality, and the == equality operator performs type conversions
as described in Conversions and Equality.

JavaScript variables are untyped: you can
assign a value of any type to a variable, and you can later assign a
value of a different type to the same variable. Variables are
declared with the var keyword. JavaScript uses
lexical scoping. Variables declared outside of a
function are global variables and are visible
everywhere in a JavaScript program. 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 Variable Declaration and Variable Scope.

Comments are closed.