Assignment by Value Versus by Reference : Assignment « Operators « JavaScript Tutorial

Home
JavaScript Tutorial
1.Language Basics
2.Operators
3.Statement
4.Development
5.Number Data Type
6.String
7.Function
8.Global
9.Math
10.Form
11.Array
12.Date
13.Dialogs
14.Document
15.Event
16.Location
17.Navigator
18.Screen
19.Window
20.History
21.HTML Tags
22.Style
23.DOM Node
24.Drag Drop
25.Object Oriented
26.Regular Expressions
27.XML
28.GUI Components
29.Dojo toolkit
30.jQuery
31.Animation
32.MS JScript
JavaScript Tutorial » Operators » Assignment 
2.4.7.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>
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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.