3.1 NumbersNumbers 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 floating-point values. All numbers in JavaScript are represented as floating-point values. JavaScript represents numbers using the 64-bit floating-point format defined by the IEEE 754 standard,[1] which means it can represent numbers as large as ±1.7976931348623157 x 10308 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.1 Integer LiteralsIn a JavaScript program, a base-10 integer is written as a sequence of digits. For example: 0 3 10000000 The JavaScript number format allows you to exactly represent all integers between -9007199254740992 (-253) and 9007199254740992 (253), inclusive. If you use integer values larger than this, you may lose precision in the trailing digits. Note, however, that certain integer operations in JavaScript (in particular the bitwise operators described in Chapter 5) are performed on 32-bit integers, which range from -2147483648 (-231) to 2147483647 (231 -1). 3.1.2 Hexadecimal and Octal LiteralsIn addition to base-10 integer literals, JavaScript recognizes hexadecimal (base-16) values. A hexadecimal literal begins with "0x" or "0X", followed by a string of hexadecimal digits. A hexadecimal digit is one of the digits 0 through 9 or the letters a (or A) through f (or F), which are used to represent values 10 through 15. Examples of hexadecimal integer literals are: 0xff // 15*16 + 15 = 255 (base 10) 0xCAFE911 Although the ECMAScript standard does not support them, some implementations of JavaScript allow you to specify integer literals in octal (base-8) format. An octal literal begins with the digit 0 and is followed by a sequence of digits, each between 0 and 7. For example: 0377 // 3*64 + 7*8 + 7 = 255 (base 10) Since some implementations support octal literals and some do not, you should never write an integer literal with a leading zero -- you cannot know whether an implementation will interpret it as an octal or decimal value. 3.1.3 Floating-Point LiteralsFloating-point literals can have a decimal point; they use the traditional syntax for real numbers. A real value is represented as the integral part of the number, followed by a decimal point and the fractional part of the number. Floating-point literals may also be represented using exponential notation: a real number followed by the letter e (or E), followed by an optional plus or minus sign, followed by an integer exponent. This notation represents the real number multiplied by 10 to the power of the exponent. More succinctly, the syntax is: [digits][.digits][(E|e)[(+|-)]digits] For example: 3.14 2345.789 .333333333333333333 6.02e23 // 6.02 x 1023 1.4738223E-32 // 1.4738223 x 10-32 Note that there are infinitely many real numbers, but only a finite number of them (18437736874454810627, to be exact) can be represented exactly by the JavaScript floating-point format. This means that when you're working with real numbers in JavaScript, the representation of the number will often be an approximation of the actual number. The approximation is usually good enough, however, and this is rarely a practical problem. 3.1.4 Working with NumbersJavaScript 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 ValuesJavaScript uses several special numeric values. When a floating-point 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 not-a-number value, printed as NaN. The not-a-number 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 3-1 lists several constants that JavaScript defines to represent these special numeric values.
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. |