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

Notice that the exponent specification can be in uppercase or lowercase characters.

Floating point math is much more difficult than integer math and is the source of many bugs. Overflow describes the situation in which the exponent of a floating point number exceeds the upper limit. Underflow occurs when the exponent is below the lower one. You should avoid calculations that are likely to result in an overflow or an underflow.

A more common problem with floating point numbers is the roundoff error. Everyone knows that 5 + 5 is 10, but is 1/3 + 1/3 equal to 2/3? The answer is: not always. Take a look at the following listings to find out why:

1/3 as floating point is 3.333E–1 (rounded-off).
2/3 as floating point is 6.667E–1 (rounded-off).

3.333E–1
 +   3.333E–1
 =   6.666E–1

As you can see, the result is different from the normal value of 2/3 because 6.666E–1[not equal to]6.667E–1.

Every computer has a similar problem with its floating point. The fraction 0.2, for example, has no exact binary representation. Avoid computing money with floating point numbers, because financial institutions, such as the IRS, tend to be fussy about money and will probably not tolerate an inaccurate payment!

Accuracy is a major problem with floating point numbers. Certain operations, such as subtracting two close numbers, generate inexact results. Some advanced techniques are known to work around such problems, but they are usually targeted for a specific machine, operating system, or browser. Since your scripts need to be compatible with several different browsers, operating systems, and machines, you should avoid getting into trouble with floating point numbers altogether. If your computation involves floating numbers, always round off the result, so it is not influenced by the floating point storage problem. You will also learn to use comparison techniques that take inexact results into consideration. These topics are discussed later.

Boolean Literals

Boolean values, also called logical values, are basically true or false. They are usually used in conditional expressions and statements (presented in Chapter 8, Control Structures). If you are familiar with C++, you probably recognize these terms as 1 and 0, 1 representing true and 0 representing false. JavaScript officially uses the true and false values to express Boolean values, but 1 and 0 are acceptable in most situations. The true value, or 1, can usually be replaced by any nonzero integer. Avoid using these numeric values as Boolean, because Netscape has not officially recognized this kind of usage and may opt to invalidate it in future versions of the browser. Usage of integer values to represent Boolean ones, as in other programming languages, can cause data type confusion and should not be used at all by novice programmers.

String Literals

String literals are delimited by either single or double quotes. You must terminate the string with the same type of quote you used to open it, so "Hi' is not a legal string in JavaScript. Unlike strings in most programming languages and shells, JavaScript does not distinguish between single and double quotes. They serve the exact same purposes. Strings in JavaScript are not subject to variable interpolation, i.e., you cannot embed variables directly in the string and expect them to be replaced by the value they hold. Perl, for example, features variable interpolation (or variable substitution) because variables can be identified by the preceding $ character.

The alternative delimiters q/string/ and qq/string/ are not supported in JavaScript. You must always use the traditional quotes to delimit strings.

Nested strings are widely used in JavaScript. A nested string consists of a string inside another one. Alternating quotes enables proper interpretation of nested string constructions. The following statement demonstrates how to alternate quote types:

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

The document.write() statement requires quotes, and so does the COLOR attribute of the <FONT> tag. You may use single quotes for the string red and double quotes for the longer enclosing string. You may also use escaped quotes, as explained below.

Be careful to place the trailing quote at the end of the string. If you forget it, the relevant error will be reported only after JavaScript runs into the end of the file or finds a matching quote character in another line. Fortunately, such errors will be detected immediately as syntax errors on the following line. Sometimes, though, the error message does not give any clue about the runaway string, so paying extra attention when dealing with strings is a profitable strategy.

Strings often include escape sequences, also called escape characters, special characters, or control characters. Such sequences have special purposes. For example, “\t” is the tab character. They are usually used to express nonprintable characters or other problematic ones. The following table outlines the escape sequences:

Table 5-3. Escape sequences in JavaScript.


Escape sequence Character Meaning

\ddd* 0ddd octal character
\xdd* 0xdd hexadecimal character
\\ \ backslash
\' ' single quote
\" " double quote
\b BS backspace
\f FF form feed
\n NL or LF new line
\r CR carriage return
\t HT horizontal tab
\ <new line> continuation

* The “d” character represents a digit.

Be sure to use these inline characters only where needed. You should use the standard HTML tags for line breaks (<BR>) and paragraph breaks (<P>). The carriage return escape sequence creates a new line only in dialog boxes and text area objects. Most of the escape sequences act the same way. Do not expect them to format the look of the page, because layout relies solely on HTML.

Here are some strings that take advantage of escape sequences:

"\x2499.99" // $99.99 (the hex value of the $ char is 24)
'c:\\games\\sc2000\\' // c:\games\sc2000\
'Let\'s learn JavaScript...' // Let's learn JavaScript...
"line1\rline2"
Previous Table of Contents Next


With any suggestions or questions please feel free to contact us