Chapter 3. Data Types and Values
Computer programs work by manipulating
values
,
such as the number 3.14 or the text "Hello World". The
types of values that can be represented and manipulated in a
programming language are known as data types,
and one of the most fundamental characteristics of a programming
language is the set of data types it supports. JavaScript allows you
to work with three
primitive
data types:
numbers, strings of text
(known as "strings"), and boolean truth values (known as
"booleans"). JavaScript also defines two trivial data
types, null and undefined, each of which defines only a single value.
In addition to these primitive data types, JavaScript supports a
composite data type known as
object. An
object (that is, a member of the data type object) represents a
collection of values (either primitive values, like numbers and
strings, or composite values, like other objects). Objects in
JavaScript have a dual nature: an object can represent an unordered
collection of named values or an ordered collection of numbered
values. In the latter case, the object is called an
array
.
Although objects and arrays are fundamentally the same data type in
JavaScript, they behave quite differently and will usually be
considered distinct types throughout this book.
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 perform some kind of operation.
Like arrays, functions behave differently from other kinds of
objects, and JavaScript defines special language syntax for working
with them. Thus, we'll treat the function data type
independently of the object and array types.
In addition to functions and arrays, core JavaScript defines a few
other specialized kinds of objects. These objects do not represent
new data types, just new classes of objects. The
Date
class defines objects that represent dates, the RegExp class defines
objects that represent regular expressions (a powerful
patternmatching tool described in
Chapter 10), and the
Error
class defines objects that represent syntax and runtime errors that
can occur in a JavaScript program.
The remainder of this chapter documents each of the primitive data
types in detail. It also introduces the object, array, and function
data types, which are fully documented in Chapter 7,
Chapter 8, and Chapter 9.
Finally, it provides an overview of the Date, RegExp, and Error
classes, which are documented in full detail in the core reference
section of this book.
3.1. Numbers
Numbers
are
the most basic data type; they require very little explanation.
JavaScript differs from programming languages such as C and Java in
that it does not make a distinction between integer values and
floatingpoint values. All numbers in JavaScript are represented as
floatingpoint values. JavaScript represents numbers using the
64bit floatingpoint format defined
by the IEEE 754 standard,[5] which
means it can represent numbers as large as ±1.7976931348623157
x 10^{308} and as small as ±5
x 10^{ 324. }
When a number appears directly in a JavaScript program, we call it a
numeric
literal. JavaScript supports numeric literals in several formats, as
described in the following sections. Note that any numeric literal
can be preceded by a minus sign () to make the number negative.
Technically, however,  is the unary negation operator (see Chapter 5), not part of the numeric literal syntax.
3.1.4. Working with Numbers
JavaScript programs work with numbers using
the
arithmetic
operators that the language provides. These include
+ for addition,  for
subtraction, * for multiplication, and
/ for division. Full details on these and other
arithmetic operators can be found in Chapter 5.
In addition to these basic arithmetic operations, JavaScript
supports more complex mathematical
operations through a large number of mathematical functions that are
a core part of the language. For convenience, these functions are all
stored as properties of a single Math object, so we always use the
literal name Math to access them. For example,
here's how to compute the sine of the numeric value
x:
sine_of_x = Math.sin(x);
And to compute the square root of a numeric expression:
hypot = Math.sqrt(x*x + y*y);
See the Math object and subsequent listings in the core reference
section of this book for full details on all the mathematical
functions supported by JavaScript.
There is also one interesting method that you can use with numbers.
The toString( )
method converts an integer to a string,
using the radix, or base, specified by its argument (the base must be
between 2 and 36). For example, to convert a number to
binary, use
toString( ) like this:
var x = 33;
var y = x.toString(2); // y is "100001"
To invoke the toString( ) method on a number
literal, you can use parentheses to prevent the .
from being interpreted as a decimal point:
var y = (257).toString(0x10); // y is "101"
3.1.5. Special Numeric Values
JavaScript uses
several special
numeric
values. When a floatingpoint value
becomes larger than the largest representable finite number, the
result is a special infinity value, which JavaScript prints as
Infinity. Similarly, when a negative value becomes
lower than the last representable negative number, the result is
negative infinity, printed as
Infinity.
Another
special JavaScript numeric value is returned when a mathematical
operation (such as division of zero by zero) yields an undefined
result or an error. In this case, the result is the special
notanumber value, printed as NaN. The
notanumber value behaves unusually: it does not compare equal to
any number, including itself! For this reason, a special function,
isNaN( ), is required to test for this value. A
related function, isFinite(
) , tests whether a number is not NaN and is
not positive or negative infinity.
Table 31 lists several
constants that JavaScript defines
to represent these special numeric values.
Table 31. Special numeric constants
Constant

Meaning

Infinity

Special value to represent infinity

NaN

Special notanumber value

Number.MAX_VALUE

Largest representable number

Number.MIN_VALUE

Smallest (closest to zero) representable number

Number.NaN

Special notanumber value

Number.POSITIVE_INFINITY

Special value to represent infinity

Number.NEGATIVE_INFINITY

Special value to represent negative infinity

The Infinity and NaN constants
are defined by the ECMAScript v1 standard and are not implemented
prior to JavaScript 1.3. The various Number
constants, however, have been implemented since JavaScript
1.1.
   2.8. Reserved Words   3.2. Strings 
Copyright © 2003 O'Reilly & Associates. All rights reserved.

