
LiteralsLiterals are fixed values that you literally provide in your application source, and are not variables or any other data structure. They are notations for constant values and therefore do not change throughout the script. A literal gives you a value instead of merely representing possible values. Unlike C++ and Java, which have five literal types, JavaScript has only four: integer, floating point, Boolean, and string. Character literals are considered strings. Integer LiteralsInteger literals, also called whole numbers, are numbers that have no decimal point or fractional part. Here are some integer literals:
49 16 0 –18 –42
An integer can be positive, negative, or zero. JavaScript, like most other programming languages, supports integers of three types or bases: decimal, octal, and hexadecimal. Decimal IntegersDecimal integers, also known as base10 integers, are the common integers we use daily. They are written with the digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. Except for the number 0 itself, a decimal integer cannot have a leading 0 digit. It makes no sense to write the current year as 01997, and, indeed, JavaScript will not evaluate it as 1997. Octal IntegersOctal integers, also known as base8 integers, use only eight digits: 0, 1, 2, 3, 4, 5, 6, 7. Octal digits are written with a leading 0 digit (that’s a zero, not the letter “o”). If you want to reference the octal integer 12 (equal to 10 in the decimal system), you would have to write it as 012. Hexadecimal IntegersHexadecimal integers are commonly used in programming environments because each hexadecimal digit represents four binary bits. See the section “Bitwise Operators” in Chapter 7, Utilizing JavaScript Operators, for more details on internal storage and hexadecimal representations. A hexadecimal number is any sequence of these digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. The integers are called hexadecimal because they are based on 16 different digits. Case sensitivity rules do not apply to numbers, so you can use lowercase letters as well. The prefix for hexadecimal numbers is 0x or 0X. Hexadecimal numbers might look familiar to HTML authors. In early versions of Netscape Navigator and MSIE, colors were specified in a hexadecimal triplet format. Although the latest versions of Netscape Navigator and MSIE let you specify colors by their names, some people continue to use the hexadecimal notation. The following tag sets the background color to the default gray (even for users who changed their default to white):
<BODY BGCOLOR="#c0c0c0">
You probably know that colors differ in the relative contributions of red, green, and blue. The hexadecimal triplet combines three hexadecimal numbers of two digits each (2 digits = 16 * 16 = 256 possibilities). Converting from Decimal to Other BasesYou can use a calculator to convert a decimal integer to a different base. But what happens when the calculator is broken?! It is surprisingly simple to do the conversion with a pencil and a piece of paper. You start by dividing the decimal number by the target base (e.g., 2 for binary, 8 for octal, 16 for hexadecimal). Write down the remainder. Now do the same with the quotient, writing the new remainder to the left of that from the previous operation. Keep looping until the quotient is less than the target base. The following table shows the conversion of the decimal number 747 to a hexadecimal notation: Table 52. A base conversion example.
Take a look at the table’s far right column. Reading it bottom up, we get 2EB_{16}, which is exactly 747_{10}. The same process converts decimal integers to any base. Converting from a Specified Base to DecimalIt is equally easy to convert a number from a specified base to a decimal notation. First, let’s analyze a number in decimal notation, say 276:
276 = (6 * 10^{0}) + (7 * 10^{1}) + (2 * 10^{2}) = 6 + 70 + 200
Each digit contributes the value digit * base^{digitPlaceFromRight}, where digit is the current digit, digitPlaceFromRight is the digit’s position (starting at the righthand side 0 position), and base is the current base. The number is equal to the sum of all contributed values. Conversion from a given base to a decimal notation uses the same technique, except that each digit is worth its value in decimal notation. Let’s convert 1472_{8} (octal) to a decimal base:
(2 * 8^{0}) + (7 * 8^{1}) + (4 * 8^{2})+ (1 * 8^{3}) = 2 + 56 + 256 + 512 = 826_{10}
We use these techniques to create automatic conversion functions later in the book. Referring to Octal and Hexadecimal IntegersOctal integers have a leading 0 character. Hexadecimal integers have a leading 0x or 0X prefix. Decimal integers are normal integers with no prefixes. So how do we work with these prefixes? JavaScript refers to all integers as decimal. So if you write 0x2EB, JavaScript stores the number and refers to it as 747 in decimal notation. The same rule applies also to octal integers. They are stored, displayed, and handled according to their decimal value. The computer obviously stores them as binary numbers, but to the JavaScript programmer it seems as if they are handled in decimal form. Floating Point LiteralsFloating point numbers (sometimes called real) have a fractional part. JavaScript uses a decimal point to distinguish between floating point numbers and integers. Literally, a number such as 16.0 is a floating point number, whereas 16 is an integer one. Additionally, a floating point number may include an exponent specification of the form e±exp. Since there is no explicit declaration of data types in JavaScript, the floating point format is implicitly determined by the literals that are assigned to the variables. Here are some floating point numbers, with some explanations:
–13.3 0.056 4.2e19 // equal to 4.2 * 10 –3.1E12 // equal to –3.1 * 10 .1e12 // equal to 0.1 * 10 2E–12 // equal to 2 * 10


With any suggestions or questions please feel free to contact us 