Friday 24 August 2018

JavaScript: Compound Assignment operator

JavaScript supports compound assignments
   Compound Assignment
      Syntax:
         variable operator= value

      Example:
         lets say
         var a = 10;
         a+=10 evaluates to a=a+10

Compound Operator
Example
Description
+=
a+=10
a=a+10
-=
a-=10
a=a-10
*=
a*=10
a=a*10
/=
a/=10
a=a/10
>>=
a>>=10
a=a>>10
<<=
a<<=10
a=a<<10
>>>=
a>>>=10
a=a>>>10
&=
a&=10
a=a&10
|=
a|=10
a=a|10
^=
a^=10
a=a^10

compound.html

<!DOCTYPE html>

<html>

<head>
    <title>Compound Operators</title>

    <style>
        p {
            font-size: 2em;
            color: seagreen;
        }
    </style>
</head>

<body>
    <p>
        <script type="text/javascript">
            var a = 7;

            document.write("a = " + a + "<br />");
            document.write("(a+=10) = " + (a += 10) + "<br />");

            a = 7;
            document.write("(a-=10) = " + (a -= 10) + "<br />");

            a = 7;
            document.write("(a*=10) = " + (a *= 10) + "<br />");

            a = 7;
            document.write("(a/=10) = " + (a /= 10) + "<br />");

            a = 7;
            document.write("(a%=10) = " + (a %= 10) + "<br />");

            a = 7;
            document.write("(a>>=10) = " + (a >>= 10) + "<br />");

            a = 7;
            document.write("(a<<=10) = " + (a <<= 10) + "<br />");

            a = 7;
            document.write("(a>>>=10) = " + (a >>>= 10) + "<br />");

            a = 7;
            document.write("(a&=10) = " + (a &= 10) + "<br />");

            a = 7;
            document.write("(a|=10) = " + (a |= 10) + "<br />");

            a = 7;
            document.write("(a^=10) = " + (a ^= 10) + "<br />");
        </script>
    </p>
</body>

</html>




Previous                                                 Next                                                 Home

No comments:

Post a Comment