Operators can be placed into three categories-binary, unary, and ternary. Binary operators, most commonly associated with the concept of operator, take two (binary) expressions and combine them into a third complex or compound expression. However, a single expression can have several binary operators. For example, the following variable declaration uses multiple binary operators to define the variable: var calcAdd = (total / n ) + 73 The divide (/) operator and the plus (+) operator are binary operators. The first combination occurs when the variable total is divided by the variable n. The two variables become a single value. That single value resulting from total divided by n is then added to the literal numeric value of 73, creating yet another value. The equals sign (=) places the combined value of the operands into the variable calcAdd. Unary operators work on a single variable or literal. All negative numbers are assigned using a unary operator. For example, the following little script uses a unary operator to create a variable with a negative value: <html> <head> <script language="JavaScript"> var posNum=85; var negNum= -posNum; document.write(negNum); </script> </head> <body bgcolor="honeydew"> </body> </html> The return of the script is -85 because the minus (-) unary operator defined the variable negNum as the negation of the variable posNum. Other common unary operators include increment or decrement operators (++ and - -) seen in counter variables. Finally, ternary operators combine three expressions into one. Most commonly used to create a shorthand expression for conditional statements, the only ternary operator in JavaScript is ? :. For example, this conditional statement: if(alpha == beta) {
gamma=56;
} else {
gamma=57;
}
can be written with a ternary operator as follows: alpha == beta ? gamma=56 : gamma=57; The following little script shows how both methods arrive at the same conclusion: <html>
<head>
<script language="JavaScript">
var alpha=20, beta=30, gamma=0, lambda=0;
if (alpha==beta) {
var gamma=56;
} else {
gamma=57;
}
//Same set of conditions using ternary operator
alpha==beta ? lambda=56 : lambda=57;
document.write("Conditional results:" + gamma + "<p>" + "Ternary conditional:" + lambda);
</script>
</head>
<body bgcolor="oldlace">
</body>
</html>
The three elements that the ?: operator brought together in the example are (alpha==beta), (lambda=56), and (lambda=57). Note also how the comma (,) operator is used in the script to separate the definitions of the variables alpha, beta, gamma, and lambda at the beginning of the script.
|
|||||||||||||
Disclaimer
1) E-articles is not responsible for the information contained by this article as well for any and all copyright infringements by authors and writers. E-articles is a free information resource. If you suspect this article for any copyright infringement, please read the terms of service and contact us or use the "Report this article" button on this page to investigate the problem.
2) E-articles is not responsible for inaccuracies, falsehoods, or any other types of misinformation this article may contain and will not be liable for any loss or damage suffered by a user through the user's reliance on the information gained here. |
|||||||||||||