HTML and CSS Reference
In-Depth Information
The shortcut assignment operators allow you to perform an arithmetic or string opera-
tion by combining an assignment operator with an arithmetic or string operator. For
example,
x = x + 1
can be written
x+=1
. See Table 5.3.
Table 5.3
Assignment Operators
Operator
Example
Meaning
=
var x = 5;
Assign
5
to variable
x.
+=
x += 3;
Add
3
to
x
and assign result to
x.
-=
x -= 2;
Subtract
2
from
x
and assign result to
x.
*=
x *= 4;
Multiply
x
by
4
and assign result to
x.
/=
x /= 2;
Divide
x
by
2
and assign result to
x.
**=
x **= 2;
Square
x
and assign result to
x
.
%=
x %= 2;
Divide
x
by
2
and assign remainder to
x.
EXAMPLE
5.4
<html>
<head>
<title>Assignment and Shortcut Operators</title>
</head>
<body bgcolor="gold">
<big>
1
<script type= "text/javascript">
2
var num=10;
3
document.write("num is assigned " + 10);
4
num += 2;
document.write("<br />num += 2; num is " + num );
5
num -= 1;
document.write("<br />num -= 1; num is " + num);
6
num *= 3;
document.write("<br />num *= 3; num is " + num);
7
num %= 5;
document.write("<br />num %= 5; num is " + num);
8
</script>
</big>
</body>
<html>