home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Listing 18.1 VarType.htmJavaScript Enables You to Change the Data Type of Variables


<HTML>
<HEAD>
<SCRIPT LANGUAGE=”JavaScript”&g
<!-- Hide this script from incompatible Web browsers!
function typedemo() {
   var x;
   document.writeln(“<DL>”);
   document.writeln(“<DT>Undefined...</DT>”);
   document.writeln(“<DD>x = “ + x + “</DD>”);
   document.writeln(“<DT>&nbsp</DT>”);
   x = 17;
   document.writeln(“<DT>Integer...</DT>”);
   document.writeln(“<DD>x = “ + x + “</DD>”);
   document.writeln(“<DT>&nbsp</DT>”);
   x = Math.PI;
   document.writeln(“<DT>Floating-Point...</DT>”);
   document.writeln(“<DD>x = “ + x + “</DD>”);
   document.writeln(“<DT>&nbsp</DT>”);
   x = ‘Hi, Mom!’;
   document.writeln(“<DT>String...</DT>”);
   document.writeln(“<DD>x = “ + x + “</DD>”);
   document.writeln(“<DT>&nbsp</DT>”);
   x = false;
   document.writeln(“<DT>Boolean...</DT>”);
   document.writeln(“<DD>x = “ + x + “</DD>”);
   document.writeln(“</DL>”);
}
//   Hide this script from incompatible Web browsers! -->
</SCRIPT>
<TITLE>Changing Data Types On-The-Fly...</TITLE>
</HEAD>
<BODY BGCOLOR=#FFFFFF>
<H1>Changing Data Types On-The-Fly...</H1>
<HR>
<SCRIPT LANGUAGE=”JavaScript”>
<!-- Hide this script from incompatible Web browsers!
typedemo();
//   Hide this script from incompatible Web browsers! -->
</SCRIPT>
<HR>
<A HREF=”mailto:odonnj@rpi.edu”><EM>Jim O’Donnell</EM></A>
</BODY>
</HTML>


NOTE:  Math is a JavaScript object used to access many of its math functions. The next chapter introduces you to more of the Math object’s properties.

See “JavaScript Objects,” p. 479.

This short program shows that you can use the same variable in JavaScript to represent any or all of the different data types. If you tried to do something like this in most other languages, you would either generate a compiler error or a runtime error. JavaScript happily accepts the change and prints x’s new value at each step (see Figure 18.1).

The most common assignment operator, =, assigns the value of an expression’s right side to its left side. In the preceding example, the variable x got the floating-point value of 3.141592653589793 or the Boolean value of FALSE after the expression was evaluated. For convenience, JavaScript also defines some other operators that combine common math operations with assignment. Table 18.1 shows these.

Table 18.1 Assignment Operators that Provide Shortcuts to Doing Assignments and Math Operations at the Same Time

Operator What It Does Two Equivalent Expressions

+= adds two values x+=y and x=x+y
adds two strings string += “HTML” and string = string + “HTML”
-= subtracts two values x-=y and x=x-y
*= multiplies two values a*=b and a=a*b
/= divides two values e/=b and e=e/b


FIGURE 18.1  Because JavaScript variables are loosely typed, not only their value can be changed, but also their data type.

Math Operators The previous sections gave you a sneak preview of the math operators that JavaScript furnishes. You can either combine math operations with assignments, as shown in Table 18.1, or use them individually. As you would expect, the standard four math functions (addition, subtraction, multiplication, and division) work the same as they do on an ordinary calculator. The negation operator, -, is a unary operator that negates the sign of its operand. Another useful binary math operator is the modulus operator, %. This operator returns the remainder after the integer division of two integer numbers. For instance, in the expression

x = 13%5;

the variable x would be given the value of 3.

JavaScript also adds two useful unary operators, -- and ++, called, respectively, the decrement and increment operators. These two operators modify the value of their operand, and they return the new value. They also share a unique property: You can use them either before or after their operand. If you put the operator after the operand, JavaScript returns the operand’s value and then modifies it. If you take the opposite route and put the operator before the operand, JavaScript modifies it and returns the modified value. The following short example might help to clarify this seemingly odd behavior:

x = 7;   // set x to 7
a = --x; // set x to x-1, and return the new x; a = 6
b = a++; // set b to a, so b = 6, then add 1 to a; a = 7
x++;     // add one to x; ignore the returned value

Comparison Operators Comparing the value of two expressions to see whether one is larger, smaller, or equal to another is often necessary. JavaScript supplies several comparison operators that take two operands and return TRUE if the comparison is true and FALSE if it is not. (Remember, you can use literals, variables, or expressions with operators that require expressions.) Table 18.2 shows the JavaScript comparison operators.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.