JavaScript DHTML/Language Basics/Operator — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 09:59, 26 мая 2010
Содержание
- 1 Add two integer variables together
- 2 Examples of Increment and Decrement Operators
- 3 JavaScript compound divide assignment
- 4 JavaScript compound multiply assignment
- 5 JavaScript compound plus assignment
- 6 JavaScript compound subtraction assignment
- 7 Modular operation (remainder in division)
- 8 Multiply two integer variables together
- 9 Operators and Expressions:Operator Summary Table
- 10 Postfix adding
- 11 Postfix operator for integer
- 12 Postfix subtraction
- 13 Prefix operator for integer
- 14 Prefix plus
- 15 Prefix subtraction
- 16 Subtract one integer variable from another integer variable
- 17 Tenary operator
- 18 Ternary operator
- 19 Using the Equal Operator
- 20 Working with the Increment Operator
Add two integer variables together
<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
var a = 6, b = 4;
alert(a + b);
</script>
</head>
<body>
</body>
</html>
Examples of Increment and Decrement Operators
<html>
<head>
<title>JavaScript Unleashed</title>
</head>
<body>
<script type="text/javaScript">
<!--
var i = 0;
var result = 0
document.write("If i = 0, ");
document.write("i returns the value of i");
document.write(" after incrementing : ");
// Increment prefix
result = ++i;
document.write(result);
i = 0;
document.write("<br>i++ returns the value of i");
document.write(" before incrementing : ");
// Increment suffix
result = i++;
document.write(result);
i = 0;
document.write("<br>--i returns the value of i");
document.write(" after decrementing : ");
// Decrement prefix
result = --i;
document.write(result)
i = 0;
document.write("<br>i-- returns the value of i");
document.write(" before decrementing : ");
// Decrement suffix
result = i--;
document.write(result);
// -->
</script>
</body>
</html>
JavaScript compound divide assignment
<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
var x = 6;
alert(x /= 2);
</script>
</head>
<body>
</body>
</html>
JavaScript compound multiply assignment
<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
var x = 6;
alert(x *= 2);
</script>
</head>
<body>
</body>
</html>
JavaScript compound plus assignment
<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
var x = 6;
alert(x += 2);
</script>
</head>
<body>
</body>
</html>
JavaScript compound subtraction assignment
<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
var x = 6;
alert(x -= 2);
</script>
</head>
<body>
</body>
</html>
Modular operation (remainder in division)
<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
var a = 6, b = 4;
alert(a % b);
</script>
</head>
<body>
</body>
</html>
Multiply two integer variables together
<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
var a = 6, b = 3;
alert(a * b);
</script>
</head>
<body>
</body>
</html>
Operators and Expressions:Operator Summary Table
<html>
<head>
<title>JavaScript Operators</title>
</head>
<body>
<h1>JavaScript Operators</h1>
<table BORDER="2" CELLPADDING="4" ALIGN="CENTER">
<tr><td>Category</td>
<td>Operator</td>
<td>Description</td>
<td>Usage Example</td>
<td>Value/Result</td></tr>
<tr><td>String</td>
<td>+</td>
<td>concatenation</td>
<td>"Java" + "Script"</td>
<td><script><!--
document.write("Java"+"Script")
// --></script>
</td></tr>
<tr><td ROWSPAN="10">Arithmetic</td>
<td>+</td>
<td>addition</td>
<td>3 + 3</td>
<td><script><!--
document.write(3+3)
// --></script>
</td></tr>
<tr><td ROWSPAN="2">-</td>
<td>subtraction</td>
<td>6 - 2</td>
<td><script><!--
document.write(6-2)
// --></script>
</td></tr>
<tr><td>unary negation</td>
<td>-4</td>
<td><script><!--
document.write(-4)
// --></script>
</td></tr>
<tr><td>*</td>
<td>multiplication</td>
<td>4 * 4</td>
<td><script><!--
document.write(4*4)
// --></script>
</td></tr>
<tr><td>/</td>
<td>division</td>
<td>15/5</td>
<td><script><!--
document.write(15/5)
// --></script>
</td></tr>
<tr><td>%</td>
<td>modulus</td>
<td>22%7</td>
<td><script><!--
document.write(22%7)
// --></script>
</td></tr>
<tr><td ROWSPAN="2">++</td>
<td>increment and then return value</td>
<td>x=4; ++x</td>
<td><script><!--
x=4
document.write(++x)
// --></script>
</td></tr>
<tr><td>return value and then increment</td>
<td>x=4; x++</td>
<td><script><!--
x=4
document.write(x++)
// --></script>
</td></tr>
<tr><td ROWSPAN="2">--</td>
<td>decrement and then return value</td>
<td>x=3; --x</td>
<td><script><!--
x=3
document.write(--x)
// --></script>
</td></tr>
<tr><td>return value and then decrement</td>
<td>x=4; x--</td>
<td><script><!--
x=4
document.write(x--)
// --></script>
</td></tr>
<tr><td ROWSPAN="6">Bit Manipulation</td>
<td>&</td>
<td>and</td>
<td>11 & 7</td>
<td><script><!--
document.write(11&7)
// --></script>
</td></tr>
<tr><td>|</td>
<td>or</td>
<td>8 | 7</td>
<td><script><!--
document.write(8|7)
// --></script>
</td></tr>
<tr><td>^</td>
<td>exclusive or</td>
<td>1 ^ 7</td>
<td><script><!--
document.write(1^7)
// --></script>
</td></tr>
<tr><td><<</td>
<td>left shift</td>
<td>8 << 3</td>
<td><script><!--
document.write(8 << 3)
// --></script>
</td></tr>
<tr><td>>></td>
<td>sign-propagating right shift</td>
<td>-3 >> 2</td>
<td><script><!--
document.write(-3 >> 2)
// --></script>
</td></tr>
<tr><td>>>></td>
<td>zero-fill right shift</td>
<td>-7 >>> 3</td>
<td><script><!--
document.write(-7>>>3)
// --></script>
</td></tr>
<tr><td ROWSPAN="3">Logical</td>
<td>&&</td>
<td>logical and</td>
<td>true && false</td>
<td><script><!--
document.write(true&&false)
// --></script>
</td></tr>
<tr><td>||</td>
<td>logical or</td>
<td>true || false</td>
<td><script><!--
document.write(true||false)
// --></script>
</td></tr>
<tr><td>!</td>
<td>not</td>
<td>!true</td>
<td><script><!--
document.write(!true)
// --></script>
</td></tr>
<tr><td ROWSPAN="6">Comparison</td>
<td>==</td>
<td>equal</td>
<td>3 == 7</td>
<td><script><!--
document.write(3==7)
// --></script>
</td></tr>
<tr><td>!=</td>
<td>not equal</td>
<td>3 != 7</td>
<td><script><!--
document.write(3!=7)
// --></script>
</td></tr>
<tr><td><</td>
<td>less than</td>
<td>3 < 7</td>
<td><script><!--
document.write(3<7)
// --></script>
</td></tr>
<tr><td><=</td>
<td>less than or equal</td>
<td>3 <= 7</td>
<td><script><!--
document.write(3<=7)
// --></script>
</td></tr>
<tr><td>></td>
<td>greater than</td>
<td>3 > 7</td>
<td><script><!--
document.write(3>7)
// --></script>
</td></tr>
<tr><td>>=</td>
<td>greater than or equal</td>
<td>3 >= 7</td>
<td><script><!--
document.write(3>7)
// --></script>
</td></tr>
<tr><td>Conditional Expression</td>
<td>(condition) ? value1 : value2</td>
<td>if condition is true then value1 else value2</td>
<td>true ? 3 : 7</td>
<td><script><!--
document.write(true?3:7)
// --></script>
</td></tr>
</table>
</body>
</html>
Postfix adding
<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
var a = 6, b = 2;
alert(a++ + b);
alert(a);
</script>
</head>
<body>
</body>
</html>
Postfix operator for integer
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
var theNum = 1;
document.write(theNum+"<BR>");
document.write(theNum++);
</script>
</body>
</html>
Postfix subtraction
<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
var a = 6, b = 2;
alert(a-- + b);
alert(a);
</script>
</head>
<body>
</body>
</html>
Prefix operator for integer
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
var theNum = 1;
document.write(theNum+"<BR>");
document.write(++theNum+"<BR>");
document.write(theNum+"<BR>");
</script>
</body>
</html>
Prefix plus
<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
var a = 6, b = 2;
alert(++a + b);
</script>
</head>
<body>
</body>
</html>
Prefix subtraction
<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
var a = 6, b = 2;
alert(--a + b);
</script>
</head>
<body>
</body>
</html>
Subtract one integer variable from another integer variable
<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
var a = 6, b = 4;
alert(a - b);
</script>
</head>
<body>
</body>
</html>
Tenary operator
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
var temp = prompt("Number");
temp > 100 ? alert("> 100") : alert("< 100");
</script>
</body>
</html>
Ternary operator
<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
var w = 1, x = 2, y = 3, z = 4, ans;
ans = (w > x) ? y : z
alert(ans);
</script>
</head>
<body>
</body>
</html>
Using the Equal Operator
<html>
<head>
<title>Using the Equal Operator</title>
</head>
<body> <script type="text/javascript">
<!--
var x = 5;
var y = 5;
var z = 10;
document.write("x = " + x + "<br>");
document.write("y = " + y + "<br>");
document.write("z = " + z + "<br>");
document.write("Is x equal to y,(x == y)? ");
document.write(x == y);
document.write("<br>Is y equal to z,(y == z)? ");
document.writeln(y == z);
// -->
</script>
</body>
</html>
Working with the Increment Operator
<HTML>
<BODY>
<H1>
<SCRIPT>
var Inc = 1;
document.write("Starting value of " + Inc + "<BR>");
document.write("IncPreFix " + ++Inc + "<BR>");
document.write("IncPostFix " + Inc++ + "<BR>");
document.write("Ending value of " + Inc);
</script>
</H1>
</BODY>
</HTML>