|
Chapter 7
|
Syntax | Name | Type |
+ | Addition (plus) | Binary |
– | Subtraction (minus) | Binary |
* | Multiplication (multiply) | Binary |
/ | Division (divide) | Binary |
% | Modulus (modulo) | Binary |
++ | Increment | Unary |
–– | Decrement | Unary |
– | Negation | Unary |
Arithmetic operators take numeric literals, variables, or properties of existing objects as their operands. They always return a single numeric value, based on their operands’ values.
operand1 + operand2
The addition operator is a simple mathematical operator. It adds two numbers of any type and evaluates to their sum.
–5 + 3 // evaluates to –2 2.4 + 3.6 // evaluates to 6 1.1 + 7.8 // evaluates to 8.9
The plus operator is a bit tricky when operands are floating point numbers. This topic is covered later in the book.
operand1 – operand2
Another simple mathematical operator is the minus one. It subtracts one number from the other.
8 – 2 // evaluates to 6 16.3 – 56 // evaluates to –39.7 13.3 – 13.3 // evaluates to 0
operand1 * operand2
The * (multiplication) operator takes two numbers as its operands, and performs the usual arithmetic conversion.
4 * 3 // evaluates to 12 1.2 * 30 // evaluates to 36 20.4 * 6.7 // evaluates to 136.38
operand1 / operand2
The division operator also performs the usual arithmetic conversion. However, since JavaScript is loosely typed, this operator does not act exactly as in C, Perl, and other strictly typed programming languages. In those languages, integer division is different from floating point division in that the result of an integer division is always an integer number. JavaScript, on the other hand, does not explicitly distinguish between integers and real-valued numbers, and therefore, the result of a division operation is not guaranteed to be an integer number. In fact, most of the floating point numbers are the result of a division operator. While debugging a script, it may be helpful to remember that the division operation in JavaScript generates the same value as your pocket calculator. You should also remember that the remainder of a division operation is never discarded.
When the operands are floating point numbers and cannot be represented in binary notation, division expressions often evaluate to inaccurate results. The following code section demonstrates the behavior of JavaScript’s division operator:
3 / 4 // evaluates to 0.75 3.6 / 0.1 // evaluates to 36 –20 / 4 // evaluates to –5 11.1 / 2.22 // evaluates to 4.999999999999999
operand1 % operand2
The modulus operator returns the remainder of a division operation. The division is performed, but only the remainder is kept. The sign of the result is the sign of the quotient. The modulus operator in JavaScript is also different from the one in other programming languages. It operates not only on integers but also on floating point numbers. You should be aware that the modulus operator occasionally returns inaccurate results. The modulus’ inaccuracies stem from the division operation which sometimes returns inaccurate results:
12 % 5 // evaluates to 2 12.3 % 4 // evaluates to 0.3000000000000007 (inaccuracy) 0 % 99 // evaluates to 0 12.75 % 4.25 // evaluates to 0 11.1 % 2.22 // evaluates to 2.219999999999999
JavaScript does not feature an integral division (also called div) operator. From looking at the list of reserved words, it seems that Netscape does not plan to add it to JavaScript’s vernacular in the near future. However, it is easy to create such an operator using simple arithmetic operators. The following function demonstrates this capability:
function div(op1, op2) { return (op1 / op2 – op1 % op2 / op2) {
The keyword return instructs the function to return a value, so the function call itself evaluates to a value, just like an expression consisting of an operator.
Now we can define the newly created div syntax:
div(operand1, operand2)
Here are a few examples:
var a = div(23, 3) // a is assigned 7 var b = div(12, 4) // b is assigned 3
The function evaluates to the quotient of its arguments, with the remainder discarded. The sign of the result is the sign of the quotient.
Caution: Various operations on floating point numbers return inaccurate results. This may seem strange at the moment, but such inaccuracies are a common phenomenon in computers. You should avoid floating point values (if possible), because such inaccuracies often result in unexpected behavior. Above all, debugging scripts that fail to work due to inaccurate calculations is nearly impossible.
operand1++ ++operand1
Previous | Table of Contents | Next |