AJAX - The Complete Reference

(avery) #1

556 Part IV: Appendixes


Data Types


JavaScript’s data types are broken down into primitive and composite types. Primitive types
hold simple values and are passed to functions by value. Composite types hold heterogeneous
data (primitive and/or composite values) and are passed to functions by reference. JavaScript
is weakly typed and will commonly convert values between types to make expressions work
which may lead to errors.

Primitive Types

Five primitive types are defined, only three of which can hold useful data. These data types
are summarized in Table A-5.

Type Description Examples
Boolean Takes on one of two values: true or false. Used both as
variable values and within loops and conditions as a literal.

true
false

null Has only one value. Indicates the absence of data, for example,
it can be placed in unspecified function argument.

null

number Includes both integer and floating point types. 64-bit IEEE 754
representation. Integer ops are usually carried out using only
32 bits. Magnitudes as large as ±1.7976 × 10308 and as small
as ±2.2250 × 10 −308. Integers are considered to have a range
of 2^31 –1 to –2^31 for computational purposes. Hexadecimal
and octal forms are supported but are stored in their decimal
equivalent. You may find a special value NaN (not a number)
in the case of numeric calculation problems such as type
conversion or (0/0). You also may reach a positive or negative
infinity value. All special number cases are toxic and will
override all other values in an expression. The Number and
Math objects contain these and are other useful constants.

5
1968.38
-4.567

string Zero or more Unicode (Latin-1 prior to Netscape 6/IE4)
characters delimited by either single or double quotes. There
is no meaning difference for the type of quotes, and they are
interchangeable. The quote flexibility is useful for including
script code within markup. JavaScript supports standard C-
like escaping with a \. Commonly, you may escape quotes
with \’ and \”. Also escaping the \ is commonly performed
using \\. The whole range of escaping including common text
characters like newlines (\n) or setting particular character
codes in Latin-1 (\044), or Unicode (\u00A9) is supported.
However, given that your output environment may be an XHTML
markup document, some whitespace indications like tabs and
newlines may appear not to work.

"I am string"
'So am I'
"Say \"what\"? "
'C'
"7"
""
''
"Newline \n
Time"
"\044\044\044"
"It's unicode time
\u00A9 2007 "

undefined Has only one value and indicates that data has not yet been
assigned. For example, undefined is the result of reading a
nonexistent object property.

undefined

TABLE A-5 Primitive JavaScript Data Types
Free download pdf