Recent top five:
Let's talk about exceptions ...
How do you handle exceptions? Do you think upfront about the type of exceptions that you want to catch or do you just let
the outside world handle it?
-- Jeroen van Bergen in JW Blogs
| Enterprise AJAX - Transcend the Hype |
| Memory Analysis in Eclipse |
| Oracle Compatibility Developer's Guide |
| Memory Analysis in Eclipse |
This explanation may sound complex, but as you will soon see, an expression is really nothing more than a simple formula for addition. This column addresses the use of expressions in JavaScript, both for defining the content of variables, as well as for creating more elaborate schemes using other JavaScript constructs. Used in this way, expressions provide a way for your scripts to think on their own (although they may seem to act on their own more than you'd like them to!).
As with all modern programming languages, JavaScript expressions can be divided into several distinct categories: assignment, math, and relational. Assignment expressions assign values to variables; math expressions apply to number values only, with one exception, which we'll get into later on; and relational expressions apply to numbers and, in some cases, strings.
We're going to be examining each of these categories in detail, so we better get started. We'll begin with the assignment expressions and the operators used within these expressions.
The following table describes the assignment operators. You will probably use the = assignment operator for the bulk of your JavaScript programs, but it's nice to know the others are available in case you need them.
| Operator | Function |
| = | Assigns a value to variable (for example, Var=1) |
| += | Adds a value to a value already in a variable (for example, Var+=1) |
| -= | Subtracts a value from a value already in variable (for example, Var-=1) |
| *= | Multiplies a value with value already in a variable (for example, Var*=1) |
| /= | Divides a value with a value already in a variable (for example, Var/=1) |
| %= | Divides a value with a value already in a variable; returns the remainder (for example, Var%=1) |
Note that JavaScript supports additional operators for bitwise operations. We'll discuss bitwise operators later in this column.
The first operator in the table, the = assignment operator, lets you assign a new value to variable. If the variable previously contained a value, that value is
replaced. Take a look at the following examples: MyStringVar = "This is a string" // assign text to variable
MyNumverVar = 100 // assign number to variable
MyObjectVar = document.form[0] // assign document.form[0] object to variable
The remaining operators in the table are called shorthand assignment operators. These operators let you add, subtract, multiply, and divide values to values already in a variable.
The most commonly used shorthand assignment operator, +=, can be used in one of two ways:
+= adds the values.
+= combines them into one long string.
Here's how the += operator works when the values in question are numbers:
Var = 1; Var += 5; // Var now contains 6
You could also write the expression like this:
Var = 1; Var = Var + 5;
Now here's how the += operator works when the values in question are text strings:
Var = "Java"; Var += "Script"; // Var now contains "JavaScript
The remaining shorthand operators let you subtract, multiply, and divide values:
x -= val for subtracting x times val. This expression is equivalent to x = x - val.
x *= val for multiplying x times val. This expression is equivalent to x = x * val.
x /= val for dividing x into val. This expression is equivalent to x = x / val.
x %= val for dividing x into val, leading the remainder (modulus). This expression is equivalent to x = x % val.
Let's look at a few examples (the variable Val contains 5 in each case):
Val -= 3 // result: 2 Val *= 3 // result: 15 Val /= 3 // result: 1.666 (etc.) Val %= 3 // result: 2
That's all there is to assignment operators. Let's move on to math operators.
The following table describes the math operators, which you use to perform mathematic calculations with one or more numbers.
Note that the operands v1 and v2 are used as substitutes for actual variables/values. We'll be using this naming scheme throughout the column.
| Operator | Function |
| -value | Treats the value as a negative number |
| v1 + v2 | Adds values v1 and v2 together; can also be used to connect (concatenate) two or more strings together |
| v1 - v2 | Subtracts value v2 from v1 |
| v1 * v2 | Multiplies values v1 and v2 |
| v1 / v2 | Divides value v1 by v2 |
| v1 % v2 | Divides value v1 by v2; the result is the floating-point remainder of the division |
| v1++ | Adds 1 to v1 |
| v1-- | Subtracts 1 from v1 |
Note: In mathematical expressions, the+operator can be used in two ways. When used with numbers, the+operator adds them together. When used with strings, the+operator connects (or concatenates) the strings into a single string.
The ++ and -- operators (the increment/decrement operators borrowed from C, C++, and Java) can be used in a number of ways. The most common
is v1++ where you increment the value already in v1 by 1. (Similarly, the instruction v1-- decrements the value already in v1 by 1.) You can actually use the ++ and -- operators before or after the value:
Take a look at the following examples. In both examples the Var variable (which is the number 10) is incremented by 1. The RetVal variable, however, contains different values because of the order JavaScript uses in incrementing and returning the value.
RetVal = Var++ // returns 10 RetVal = ++Var // returns 11
The similar postfix/prefix technique works with the -- operator.
RetVal = Var-- // returns 10 RetVal = --Var // returns 9
Moving right along, we come to the relational operators.
The following table describes the relational operators, which you use to compare two values to see if they are equal, not equal, greater than, or less than (and sometimes a combination of these).