Online Documentation Server
 
ods.com.ua Web
 
Home
Programming
Net technology
Unixes
Security
RFC, HOWTO
Web technology
Data bases
Other docs

 


 

 
. , - - , .




Previous Table of Contents Next

Literals

Literals 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 Literals

Integer 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 Integers

Decimal integers, also known as base-10 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 Integers

Octal integers, also known as base-8 integers, use only eight digits: 0, 1, 2, 3, 4, 5, 6, 7. Octal digits are written with a leading 0 digit (thats 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 Integers

Hexadecimal 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 Bases

You 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 5-2. A base conversion example.


Operation and integer quotient Remainder (decimal) Remainder (hex)

747 / 16 = 46 11 B
46 / 16 = 2 14 E
no operation 2 2

Take a look at the tables far right column. Reading it bottom up, we get 2EB16, which is exactly 74710.

The same process converts decimal integers to any base.

Converting from a Specified Base to Decimal

It is equally easy to convert a number from a specified base to a decimal notation. First, lets analyze a number in decimal notation, say 276:

276 = (6 * 100) + (7 * 101) + (2 * 102) = 6 + 70 + 200

Each digit contributes the value digit * basedigitPlaceFromRight, where digit is the current digit, digitPlaceFromRight is the digits position (starting at the right-hand 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. Lets convert 14728 (octal) to a decimal base:

(2 * 80) + (7 * 81) + (4 * 82)+ (1 * 83) = 2 + 56 + 256 + 512 = 82610

We use these techniques to create automatic conversion functions later in the book.

Referring to Octal and Hexadecimal Integers

Octal 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 Literals

Floating 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 eexp. 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
2E12 // equal to 2 * 10

Previous Table of Contents Next


With any suggestions or questions please feel free to contact us