TOP - JavaScript - 演算子 - 代入演算子

代入演算子"="は、右辺値(式)を左辺値(変数・配列)に代入します。
"演算子" + "=" の形式をとる代入演算子は、演算の対象となる右辺の変数を a、左辺の値を b とした場合、

a "演算子"= b

と記述されるものは

a = a "演算子" b

と記述したものと同様の結果となります。

(例)
<SCRIPT type="text/javascript">
<!--
    function btnclick()
    {
        var s = "";

        var a = 5;
        a += 3;
        s = s + "a = 5 → a += 3 → " + a + "\n";

        a = "a";
        a += "b";
        s = s + 'a = "a" → a += "b" → ' + a + "\n";

        a = 5;
        a -= 3;
        s = s + "a = 5 → a -= 3 → " + a + "\n";

        a = 5;
        a *= 3;
        s = s + "a = 5 → a *= 3 → " + a + "\n";

        a = 10;
        a /= 2;
        s = s + "a = 10 → a /= 2 → " + a + "\n";

        a = 20;
        a %= 7;
        s = s + "a = 20 → a %= 7 → " + a + "\n";

        a = 2;
        a &= 2;
        s = s + "a = 2 → a &= 2 → " + a + "\n";

        a = 3;
        a |= 4;
        s = s + "a = 3 → a |= 4 → " + a + "\n";

        a = 7;
        a ^= 3;
        s = s + "a = 7 → a ^= 3 → " + a + "\n";

        a = 4;
        a <<= 1;
        s = s + "a = 4 → a <<= 1 → " + a + "\n";

        a = 4;
        a >>= 1;
        s = s + "a = 4 → a >>= 1 → " + a + "\n";

        a = 4;
        a >>>= 1;
        s = s + "a = 4 → a >>>= 1 → " + a + "\n";

        alert(s);
    }
//-->
</SCRIPT>


Copyright(C) 2006-2011 CLAYPOT. All Rights Reserved.