Working with JavaScript Operators

What if you wanted to manipulate your data in JavaScript code? Say, for example, that you needed to multiple 219 by 45how would you do it? In JavaScript, you can use the multiplication operator ( * ). Here's an example showing how this works:

Listing ch06_05.html
 <HTML>     <HEAD>         <TITLE>             Using Operators In JavaScript         </TITLE>     </HEAD>     <BODY>         <CENTER>             <H1>                 Using Operators In JavaScript             </H1>         </CENTER>         <SCRIPT LANGUAGE="JavaScript">  var result   result = 219 * 45   document.writeln("219 * 45 = " +  result)  </SCRIPT>     </BODY> </HTML> 

You can see the results of this code in Figure 6-5.

Figure 6-5. Using JavaScript operators in Internet Explorer.

graphics/06fig05.gif

As you might expect, there are many operators available in JavaScript. For example, you can use the increment operator, ++ , to add 1 to a numeric value. For example, if counter holds 400 , then after you apply the ++ operator, as in ++counter , counter will hold 401 .

Prefix Versus Postfix Operators

XAs C, C++, Perl, Java, and JavaScript programmers know, you can apply the increment ( ++ ) and decrement ( -- ) operators as prefix or postfix operators: ++counter or counter++ . Prefix operators are applied before the rest of the statement or expression is evaluated, and postfix operators are applied after the rest of the statement is evaluated.

These are the JavaScript operators (as with the rest of the material in this chapter, check the JavaScript documentation for more details):

  • Addition operator ( + ) Adds two numbers or concatenates two strings.

  • Assignment operator ( = ) Assigns a value or object to a variable.

  • Bitwise AND operator ( & ) Performs a bitwise AND on two values.

  • Bitwise left shift operator ( << ) Shifts the bits of an expression to the left a specified number of times.

  • Bitwise NOT operator ( ~ ) Negation. Performs a bitwise NOT on a value.

  • Bitwise OR operator ( ) Performs a bitwise OR operation on two values.

  • Bitwise right shift operator ( >> ) Shifts the bits of a value to the right (maintaining the sign of the value).

  • Bitwise XOR operator ( ^ ) Performs a bitwise exclusive OR on two values.

  • Comma operator ( , ) Causes two expressions to be evaluated sequentially.

  • Conditional (trinary) operator ( ?: ) Executes one of two expressions, depending on whether a condition is true or false. For example, a > 5 ? exp1 : exp2 executes exp1 if the value in variable a is greater than 5, and exp2 otherwise .

  • Decrement operator ( -- ) Decrements a value by 1.

  • Division operator ( / ) Divides two numbers and returns a numeric result.

  • Equality operator ( == ) Comparison operator. Compares two expressions to determine whether they are equal.

  • Greater than operator ( > ) Comparison operator. Compares two expressions to determine whether one is greater than the other.

  • Greater than or equal to operator ( >= ) Comparison operator. Compares two expressions to determine whether one is greater than or equal to the other.

  • Identity operator ( === ) Comparison operator. Compares two expressions to determine whether they are equal in value and of the same data type.

  • Increment operator ( ++ ) Increments a value by 1.

  • Inequality operator ( != ) Comparison operator. Compares two expressions to determine whether they are unequal .

  • Less than operator ( < ) Comparison operator. Compares two values to determine whether one is less than the other.

  • Less than or equal to operator ( <= ) Comparison operator. Compares two expressions to determine whether one is less than or equal to the other.

  • Logical AND operator ( && ) Performs a logical AND conjunction operation on two expressions.

  • Logical NOT operator ( ! ) Performs logical negation on an expression.

  • Logical OR operator ( ) Performs a logical OR disjunction operation on two expressions.

  • Modulus operator ( % ) Divides two numbers and returns the remainder.

  • Multiplication operator ( * ) Multiplies two numbers.

  • New operator ( new ) Creates a new object.

  • Nonidentity operator ( !== ) Comparison operator. Compares two expressions to determine whether they are equal in value or of the same data type.

  • Subtraction operator ( - ) Subtracts one value from another.

  • Typeof operator ( typeof ) Returns a string that identifies the data type of an expression.

  • Unary negation operator ( - ) Returns the negative value of a numeric expression.

  • Unsigned right shift operator ( >>> ) Performs an unsigned right shift of the bits in a value.

Besides the operators in this list, you can put together a number of combination operators from two operators. For example, counterValue += 101 adds 101 to the value in counterValue . The combination operators in JavaScript are += , -= , *= , /= , %= , &= , = , ^= , <<= , >>= , and >>>= .

It's also worth noting that a number of these operators have to do with comparisons, and you use them to make decisions in code. We've already seen the greater than comparison operator ( > ), like this:

  if(719 > 143){  document.writeln(         "The first value is greater than the second."     ) } 

What's really going on here? In this case, I'm using the if statement to compare two numbers. The if statement is fundamental to JavaScript, and it's the next step up from using simple operators.



Real World XML
Real World XML (2nd Edition)
ISBN: 0735712867
EAN: 2147483647
Year: 2005
Pages: 440
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net