17

When i started learning function in C++ its all around pass by value and reference. Is there something similar we have in javascript ?

If yes/not how it works in case of javascript?

Thanks all.

1

5 Answers 5

32

Other answers to this question are correct - all variables are passed by value in JavaScript, and sometimes that value points to an object.

When a programming language supports passing by reference, it's possible to change where the variable points from inside a function. This is never possible in JavaScript.

For example, consider this code which attempts to reassign the passed variable to a new value:

function changeCaller( x ) { x = "bar"; // Ha ha! } function testChangeCaller() { var x = "foo"; changeCaller(x); alert(x); // still "foo" } testChangeCaller(); 

Attempts to point variables at new objects fails in the same way the above example fails to reassign a primitive string:

function changeCaller( x ) { x = new Object(); // no longer pointing to the same object x.a = "bar"; } function testChangeCaller() { var x = new Object(); x.a = "foo"; changeCaller(x); alert(x.a); // still "foo" } testChangeCaller(); 

The feature which leads people to believe they're dealing with a pass-by-reference scenario is when modifying objects. You can make changes to an object or array, because the local variable points to the same object:

function changeCaller( x ) { x.a = "bar"; } function testChangeCaller() { var x = new Object(); x.a = "foo"; changeCaller(x); alert(x.a); // now "bar", since changeCaller worked on the same object } testChangeCaller(); 

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

14

JavaScript is always pass by value, never pass by reference. A lot of people confuse this because of the way objects work.

There is no "pass by reference" for any variable in JavaScript (no, not even if an object is assigned to that variable). All variables and arguments are assigned by value. If the assigned value is an object, then the value of the new variable is a reference to that object, but assigning a new value/object to the new variable will not affect the original variable.

Some people term this behaviour passing "value by reference".

A comparison - PHP

$foo = "foo"; $bar = &$foo; // assign by reference $bar = "bar"; echo $foo; // -> output: "bar" 

JavaScript

foo = {"foo": true}; bar = foo; // assign value by reference bar = {"bar": true}; alert(JSON.stringify(foo)); // -> output: '{"foo": true} 

12 Comments

For all intents and purposes, objects (including arrays) are passed by reference. Saying that "you are passing by value, but the value is a reference to the object" is another way of saying "you are passing by reference". Unless you are just trying to confuse people.
@rob, not for all intents and purposes. For example, many WMI scripting calls require that you pass a variable to the function by reference, so that the return value of that function can be an error number. Instead of returning the result, it assigns the result to the variable passed in. VBScript can handle this just fine but JScript/JavaScript cannot because it cannot pass a reference to the variable in. They are not the same thing and I'm not trying to confuse people, I'm trying unconfuse people by expelling a myth that's already caused a lot of confusion.
No, @rob, that's not the case. The value that is passed is the value of the reference. In a true pass-by-reference language, the compiler introduces a pointer that's invisible in the code. In a pass-by-reference language you can write the notorious "swap two ints" function naturally. The value of a variable in Javascript is always a reference to an object, and when you call a function you're always passing that value. If Number had methods on it that allowed the value to be changed, it would be more clear, perhaps.
@rob: Of course it will work, you misunderstood the answer and the comments. The value of the point variable is a reference to the object, but point itself is not a reference to the variable that was passed in. Pass by reference is different in the way that if you set point = "a string and not an object"; inside the function, the original variable that was passed to the function would be modified. In JavaScript this is not the case. Pass by reference and pass value by reference are not the same thing.
@joeframbach: yes. b is assigned with a reference to the same object that a also refers to. That is, both variables hold a reference to the same object, but b is not a reference to a. With true pass by reference, b = false would also change the value of a. Compare the PHP example (assign/pass by reference) to the JavaScript example (assign/pass reference by value).
|
2

Regard the discussion, the considerations about "value" and "reference" are wrong on the exception of the Pointy comment. Javascript like other reference laguages like c# or java, don't pass a referece to the variable itself to the method, but the reference of the object referenced by the variable. What the people here is expecting, is passing a pointer to the variable which is referencing the object, pass by pointer is not the same as pass by reference, and sure is not the same as pass by value.

The behavior expected here is pass by pointer. Pass by reference sends the reference to the object. Pass by value copy the object stored in the variable and pass the copy to the method.

Comments

0

The pass by value, pass by reference discussion is an evil meme. This evil meme crops up in Java discussions too.

It's simple: The bit pattern of the value is copied to the parameter. It doesn't matter if the bit pattern is an int or if it's the address of an object -- it's just copied, bit by bit, into the parameter. It couldn't be simpler. The run-time isn't doing something special for primitives versus references. It simply, and always, just makes a copy of the value.

The computer science term for this is "pass by value."

Pass by reference just isn't used in programming any more. In older languages, like Pascal, you could code a function to directly alter the value in the calling routine. So, for example, an int variable in the calling routine could be altered in the called function.

Comments

0

Primitive values are passed via value and objects are passed via reference.

Strings are immutable, so they are passed via reference although they are considered as primitive type.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.