Anil Namde
Anil Namde

Reputation: 6618

Is there thing like pass by value pass by reference in JavaScript?

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.

Upvotes: 16

Views: 13363

Answers (5)

dev1223
dev1223

Reputation: 1159

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.

Upvotes: 0

John Drake
John Drake

Reputation: 9

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.

Upvotes: 0

Andy E
Andy E

Reputation: 344723

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}

Upvotes: 14

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.

Upvotes: 2

jimbo
jimbo

Reputation: 11042

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!

Upvotes: 31

Related Questions