| [main] [misc] [graphics] [page design] [site design] [xhtml] [css] [xml] [xsl] [schema] [javascript] [php] [mysql] | |
Note that all external links will open up in a separate window. This is a stripped down version of these pages for older browsers. These pages are really meant to be viewed in a standards compliant browser. |
OperatorsThese tutorials are about JavaScript and its use for client-side Web programming. OperatorsJavaScript has many operators and many types of operators. They can be categorized as
Except for bitwise operators, we are going to look at all of these in more detail. Bitwise operators allow you to manipulate integer values at the binary level. This is not something you will need for anything but the most advanced JavaScript programs. Bitwise operations also require a strong grounding in computer logic at the level of logic gates, registers and memory addressing. We are going to stick with things you can make use of almost immediately. Arithmatic OperatorsArithmatic operators fall into two categories, those that modify the value of an expression or set of expressions and those that take the value of one expression and assign it to another expression. These latter operators are also called assignment operators. The following arithmatic operators are the basic math operators. They do not alter the values of their operands, and therefore need to have their result assigned to some variable in order to have any real effect.
The following arithmatic operators are unary operators that modify the value of their single operand.
The rest of the arithmatic operators are assignment operators. They assign the value of the operand on the right to the operand on the left. There are two types of assignment operators. The first is the simple assignment operator. It is represented by the single equals sign (
JavaScript also allows another type of operator that perform with is called assignment with operation. What this means is that they include both a math operator and an assignment operator in one operation. What is the purpose of such an operator? Well, many expressions you will end up writing may take the form of
Note that in each of these, the two characters form a single operator, and cannot have a space between them. For really intensive numeric computation, which you probably won't be doing with JavaScript, the assignment with operation statement is faster than same statement with separate computation and assignment operators. Relational OperatorsRelational operators allow you to test the relationship between two operands. They can test for equality between operands. They can compare operands to determine the relationship between them. There are even relational operators that test whether one operand is a property of another or of a given class of object. The first relational operator you should know about is the equality operator.
The equality operator is represented by the double equals sign ( The equality operator will do data type conversion if necessary.
All of the following statements equate to '1' == '1' '1' == 1 '1' == true // true equals one when converted to a number Note that objects and arrays are compared by reference. Two objects are only equal if they are both references to the same object. If you want to test the properties of two different objects for equality, then you have to test the properties themselves, not the object. Different arrays are never equal, even if they contain the same information. If you want to test the elements of the arrays for equality, you have to test the individual elements.
There is also an identity operator, represented by a triple equals sign ( '1' === '1' // true '1' === 1 // false '1' === true // false Equality operators also have their opposites.
The inequality operator ( '1' !== '1' // false '1' !== 1 // true '1' != '2' // true '1' != '1' // false (a != b) == !(a == b)
Some caveats on equality: The The next type of relational operator are the comparison operators. Comparison operators test the relationship, or relative order, of two values. These are:
The last two relational operators worth mentioning test for relationships among objects.
The
The String OperatorsThere is really only one specific string operator, although the string object has many methods you can access.
The only string operator is the concatenation operator. It is represented by the plus sign ( If you have a plus sign between a number and a string, concatenation takes precedence over addition. All other numeric operators, when one operand is a string and the other a number, will attempt to convert the string to a number. With concatenation, if one operand is a string and the other is a number, then the number will be converted to a string. This is an easy way to introduce mistakes into a program. 2 + '1' // equals '21' 2 - '1' // equals 1 When working with strings, the comparison operators test on a character by character basis, so two strings that are the same except for some trailing spaces, or a difference in capitalization, will be treated as different strings. This means that comparisons can get confusing. Note the following comparisons of 11 and 3. // compare two strings // the character in the first position // of the first string is less than the // character in the first position of the // second string, so this equates to true '11' < '3' // true // compare a string and a number // the string is converted to a number, // thus 11 is not less than three and the // result is false '11' < 3 // false // compare a word to a number // 'eleven' is not a number, which means // that the result will always equate to // false // NaN is not only not equal to anything // else, it is also not greater or less //than anything else 'eleven' < 3 // false Logical OperatorsThere are three logical operators. Logical operators work with boolean values. The double characters for AND and OR are important, otherwise the operators mean something else.
Since the expressions are evaluated in order, you can speed processing time of your scripts by coding the operand most likely to be the deciding factor first. For logical AND, if either one is false, than it returns false. Therefore, the operand most likely to be false should come first. For logical OR, if either is ture, it returns true, so it makes sense to put the operand most likely to be true first. Note that when this happens, the second operand never gets evaluated. This means you should not have any value changes in the second that may affect other parts of the code, since the expressions in the second operand may not get executed. One Last Operator
One last useful operator is the
In server-side scripting it may return other implementation dependent values.
These pages can be found at:
[http://academ.hvcc.edu/~kantopet/]
|