Arithmetic Operators


As you can see in Table 2.6, the arithmetic operators enable you to work on numeric values, adding them (with the + operator), subtracting them (with the - operator), multiplying them (with the * operator), dividing them (with the / operator), and so on. We've already see these operators at work; here's an example we saw using the / operator to convert pounds to kilograms:

 <HTML>      <HEAD>          <TITLE>              Working With Operators          </TITLE>      </HEAD>      <BODY>          <H1>Working With Operators</H1>          <SCRIPT LANGUAGE="JavaScript">          <!--  var pounds = 5.0   var kilograms = pounds / 2.2046   document.write(pounds + " pounds = " + kilograms + " kilograms.")  // -->          </SCRIPT>       </BODY>  </HTML> 

Another useful arithmetic operator is the modulus operator, % , which returns the modulus, or integer remainder, after dividing two numbers (for example, 18 % 5 = 3 ).

Besides the standard arithmetic operators + , - , * , / , and % , three more bear mention here: the increment, decrement, and unary negation operators.

The Increment Operator: ++

The increment operator, ++ , adds one to its operand. For example, if myVariable holds 10, the expression myVariable++ will increment the value in myVariable to 11. This is a handy operator, but there's something you should know here: you can use ++ before or after a variable's name , like this: ++myVariable or myVariable++ . When you use it before a variable's name, the value of the variable is incremented by one before the rest of the statement is executed; when you use it after a variable's name, the value of the variable is incremented by one after its value has been read and used in the statement. Here's an example:

(Listing 02-07.html on the web site)
 <HTML>      <HEAD>          <TITLE>              Working With the Increment Operator          </TITLE>      </HEAD>      <BODY>          <H1>Working With the Increment Operator</H1>          <SCRIPT LANGUAGE="JavaScript">          <!--  var votesDistrictOne = 5   var votesDistrictTwo = 5   document.write("The number of votes in district 1 is " + ++votesDistrictOne + graphics/ccc.gif ".")   document.write("he number of votes in district 2 is " + votesDistrictTwo++ + graphics/ccc.gif ".")  // -->          </SCRIPT>       </BODY>  </HTML> 

You can see the difference between these two ways of incrementing values in Figure 2.5. After the increments are done, both votesDistrictOne and votesDistrictTwo end up with a value of 6, but note that votesDistrictTwo is incremented only after the rest of the statement is done executing, which means its value is still displayed as 5 in Figure 2.5.

Figure 2.5. Using the increment operator.

graphics/02fig05.gif

The Decrement Operator: --

The decrement operator, -- , is just decrements its operand by one. Like ++ , it can be used before and after a variable's name, like this: --votesDistrictOne and votesDistrictOne-- ; and for these cases, the operator works just as the increment operator ++ does, except that it decrements the variable's value.

The Unary Negation Operator: -

The unary negation operator just changes the sign of its operand. (It's called the unary negation operator because it's a unary operator that just takes one operand.) If the variable temperature holds a value of -30, and you execute the JavaScript statement newTemperature = -temperature , newTemperature will be left holding 30.



Inside Javascript
Inside JavaScript
ISBN: 0735712859
EAN: 2147483647
Year: 2005
Pages: 492
Authors: Steve Holzner

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