Data Types In JavaScript
JavaScript
uses four data types numbers, strings, Boolean values, and a null value-to
represent all the information the language can handle. Compared with most
languages, this is a small number of data types, but it is sufficient to
intelligently handle most data used in everything except the most complex
programs.
Numbers
The
JavaScript numbers type encompasses what would be several types in languages
such as java. Using the numbers, it is possible to express both integers and
floating point values.
Integers
Integers
are numbers without any portion following the decimal point; that is, they are
whole numbers with no fractions. Integers can be either positive or negative
numbers. The maximum integer size is dependent on the platform running the
JavaScript application. In JavaScript, you can express integers in three
different bases: base 10 (decimal- what you normally use in everyday
situations), base 8 (octal), and base 16 (hexadecimal).
Base 8
numbers can have digits only up to 7 so that a decimal value of 18 would bean
octal value of 22. Similarly, hexadecimal allows digits up to F, where A is
equivalent to decimal 10 and F is 15. So, a decimal value of 18 would be 12 in
hexadecimal notation.
Floating
point literals must, at a minimum, include a decimal integer and either the
decimal point or the exponent indicator (e or E). As with integers, floating
point values can be positive or negative. It should be noted that JavaScript's
handling of floating point numbers can introduce inaccuracy into some
calculations. You should keep this in mind for your programs.
Strings
You
have already encountered string literals in Chapter 2, "Your First Script,"
where you used them as arguments for several methods. Technically, a string
literal contains zero or more characters enclosed, as you know, in single or
double quotes: "Hello!" or “” The last example is called the empty string. It is
important to note that the empty string
is distinct from the null value in JavaScript. Strings are different from
other data types in JavaScript. Strings are actually objects.
Boolean
A
Boolean literal can take two values: true or false. This type of literal comes
in handy when comparing data and making decisions, as you will see later in this
chapter. Unlike Java, C, and other languages, in JavaScript Boolean values can
only be represented with true and false Values of 1 and 0 are not considered
Boolean values in JavaScript.
The null
Value
The
null value is a special value in JavaScript. The null value represents just that
nothing. If you try to reference a variable that isn't defined and therefore has
no value, the value returned is the null value. Likewise, in a prompt dialog
box, if the user selects the Cancel button, a null value is returned. This is distinct from
a value of zero or an empty string where this is an actual value. The null value
is indicated in JavaScript by the term null.
NaN
In
addition to these values, some functions return a special value called NaN which
means that the value is not a number. ParseInt() and parseFloat() on UNIX
systems are examples of functions which return NaN when the argument passed to
them cannot be evaluated to a number. Values can be tested to see if they are
NaN by using the isNaN() function which returns true or false based on the
nature of the argument passed to the
function.