Chapter 3. Data Types and Values
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.1. Integer Literals
0 3 10000000
3.1.2. Hexadecimal and Octal Literals
0xff // 15*16 + 15 = 255 (base 10) 0xCAFE911
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 Literals
Floating-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:
3.14 2345.789 .333333333333333333 6.02e23 // 6.02 x 1023 1.4738223E-32 // 1.4738223 x 10-32
3.1.4. Working with Numbers
sine_of_x = Math.sin(x);
hypot = Math.sqrt(x*x + y*y);
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
Table 3-1. Special numeric constants
Copyright © 2003 O'Reilly & Associates. All rights reserved.