Assignment operator : Assignment « Operators « JavaScript Tutorial






The basic format of the assignment operator is:

x = 6;

The assignment operator can also be stacked to create simultaneous assignments.

x = y = z = 6;

Multiple assignment operators are evaluated from right to left.

y = (x = 3) + 4;

the value 3 is assigned to the variable x, which is then added to the value 4 and assigned to the variable y.

Once the expression is fully evaluated, y will contain the value 7.

<html>
<SCRIPT LANGUAGE="JavaScript">
<!--
    x = 1;
    y = 2;
    z = 3;

    document.write("<U>After single assignment</U><BR>");
    document.write("x=",x,"<BR>y=",y,"<BR>z=",z,"<BR>");

    document.write("<U>After multiple assignment</U><BR>");
    document.write("x=",x,"<BR>y=",y,"<BR>z=",z,"<BR>");

    x = (y = 17) + (2 * (z = 2));

    document.write("<U>After multiple assignment in one expression</U><BR>");

    document.write("x=",x,"<BR>y=",y,"<BR>z=",z,"<BR>");

// -->
</SCRIPT>
</html>








2.4.Assignment
2.4.1.Assignment operator
2.4.2.x += 2 (Compound Plus)
2.4.3.x -= 2 (Compound Subtraction)
2.4.4.x *= 2 (Compound Multiply)
2.4.5.x /= 2 (Multiply Divide)
2.4.6.Advanced Assignment Operators
2.4.7.Assignment by Value Versus by Reference