Assignment by Value Versus by Reference : Assignment : Operators JAVASCRIPT TUTORIALS


JAVASCRIPT TUTORIALS » Operators » Assignment »

 

Assignment by Value Versus by Reference








When the assignment operator works on primitive values (numbers, strings, Boolean, null, and undefined), a copy of the value is made.

When the assignment operator works on JavaScript objects, references to the objects are copied.












<html>
<SCRIPT LANGUAGE="JavaScript">
<!--
    var number1 = 9;
    var arrayOfNum1 = new Array(2,6);

    var number2 = number1;
    var arrayOfNum2 = arrayOfNum1;

    number2 = 2;
    arrayOfNum2[14;

    document.write("number1=",number1,"<BR>");
    document.write("number2=",number2,"<BR>");
    document.write("arrayOfNum1[0]=",arrayOfNum1[0],"<BR>");
    document.write("arrayOfNum1[1]=",arrayOfNum1[1],"<BR>");
    document.write("arrayOfNum2[0]=",arrayOfNum2[0],"<BR>");
    document.write("arrayOfNum2[1]=",arrayOfNum2[1],"<BR>");

-->

</SCRIPT>
</html>







HTML code for linking to this page:

Follow Navioo On Twitter

JAVASCRIPT TUTORIALS

 Navioo Operators
» Assignment