October 4, 1999 - Literals | WebReference

October 4, 1999 - Literals

Yehuda Shiran October 4, 1999
Literals
Tips: October 1999

Yehuda Shiran, Ph.D.
Doc JavaScript

Literals are fixed values that you use literally in your code. They are not variables or any other data structure for that matter. Literals are constant values that do not change throughout the script. A literal gives you a value instead of its address. JavaScript has four literal types: integer, floating point, Boolean, and string.

Integer literals are whole numbers that have no decimal point or fractional part. They can be positive, negative, or equal to zero. Decimal integers are written with ten digits, 0 through 9. Except for the number 0, a decimal integer cannot have a leading 0 digit.

Octal integers use eight digits, 0 through 7. Octal integers are written with a leading zero (the digit 0). The decimal integer 10 would be written as 012 in octal math.

Hexadecimal integers are commonly used in programming environment because each hexadecimal digit represents four binary bits. Hexadecimal integers are written with 16 digits, 0 through 9 and A (equal to 10) through F (equal to 15). The prefix for hexadecimal numbers is 0x or 0X. Note that number notation is case insensitive. You can see hexadecimal notation in HTML's color specification:

<BODY BGCOLOR="#c0c0c0">

There are two digits for each of the three main colors: red, green, and blue (RGB). Two digits can represent 16 * 16 = 256 different colors.

Floating point numbers (sometimes called real numbers) have a fraction part. The decimal point distinguishes between integer and floating literals. The literal 16 is an integer, while the literal 16.0 is a float. Additionally, a floating point number may include an exponent specification of the form e+exp or e-exp. Here are some floating point numbers:

-13.3
0.056
4.2e19 // equal to 4.2 * 1019
-3.1E12 // equal to -3.1 * 1012
.1e12 // equal to 0.1 * 1012
2E-12 // equal to 2 * 10-12

Boolean literals are true and false. The true value can usually be replaced by any nonzero integer, but it is not recommended. Likewise, the false value can usually be replaced by zero integer, but it is not recommended as well.

String literals are delimited by either single or double quotes. Unlike other programming languages, JavaScript does not distinguishes between single and double quotes. Nesting quotes is a common practice in JavaScript:

document.write("<FONT COLOR='red' SIZE='4'>");