0

I just can't understand how variables are passed, why are some passed by reference while other by value?

Example:

var a=4;
var b=a;
b=b++;
alert(a);//unmodified 4

var c=["t","ttt"];
var d=c;
d=d.sort(function(x,y){return (y.length-x.length);});
alert(c);//modified ["ttt","t"]

Where can I find a list of what variables will work like the first example and which like the second? (booleans, strings, etc... there are too many to test them all by miself)

ajax333221
  • 11,436
  • 16
  • 61
  • 95
  • possible duplicate of http://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language – Ray Cheng Mar 31 '12 at 03:31

4 Answers4

6

JavaScript is always pass by value.

It's very common to say that objects in JavaScript are pass by reference, however that is not true. In a true pass by reference language, you could pass a reference to an object's reference, and have it point to another object. This is not possible in JavaScript.

Take, for example, C#. By default everything in C# is pass by value, just like JavaScript.

void foo(string s) {
    s = "passbyvalue";
}

string a = "original value";
foo(a);
// a is still "original value"

However, if you alter foo to use pass by reference, the behavior changes:

void foo(ref string s) {
    s = "passbyreference";
}
string a = "original value";
foo(ref a);
// a is now "passbyreference"  

In JavaScript, only the first example is possible.

In JavaScript, everything is pass by value. This includes the object reference (yes, it is confusing). The reference itself is a value (it's very much like a pointer). It merely contains an id that allows the runtime to look up the object that is most likely stored in its heap. When you pass an object to a function, you are actually copying its reference into the function

It may be a seemingly small, and even anal, difference. But it's a key difference. Languages that have true pass by reference such as C# and C++ allow you to do things that are simply not possible in languages like JavaScript. The above trite example is one.

Matt Greer
  • 60,826
  • 17
  • 123
  • 123
3

Imagine everything in JavaScript as an object. Object references are copied by value, but object properties are still used by reference. So:

var a = {};
var b = a;
b = 'Blah';
console.log(a); // Still {}

and

var a = {};
var b = a;
b.test = 'Blah';
console.log(a); // Is now {test: 'Blah'}

To simplify things a little: properties are by reference, values are by value.

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • +1 I think this ans can finally make me understand it! I will read this like 4-5 times until it somehow gets into my brain – ajax333221 Mar 31 '12 at 03:30
1

All objects get passed by reference and values by value. (technically this is not true, but it explains the behaviour)

stewe
  • 41,820
  • 13
  • 79
  • 75
1

Usually you can check the method on MDN. For example, the sort method's documentation states (emphasis added):

Sorts the elements of an array in place and returns the array.

The "in place" there says it will modify the array.

All primitives (for example, numbers and booleans) are passed by value; all objects are passed by reference, although some (like String objects) are immutable (although still passed by reference).

icktoofay
  • 126,289
  • 21
  • 250
  • 231
  • it just had that in place issue one question ago. it would have been better if I asked this question first, so I would never had asked my previous one) – ajax333221 Mar 31 '12 at 03:36