Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is JavaScript a pass-by-reference or pass-by-value language?

The primitive types (number, string, etc.) are passed by value, but objects are unknown, because they can be both passed-by-value (in case we consider that a variable holding an object is in fact a reference to the object) and passed-by-reference (when we consider that the variable to the object holds the object itself).

Although it doesn't really matter at the end, I want to know what is the correct way to present the arguments passing conventions. Is there an excerpt from JavaScript specification, which defines what should be the semantics regarding this?

like image 604
Danail Nachev Avatar asked Feb 05 '09 21:02

Danail Nachev


People also ask

Is JavaScript is pass by value or pass by reference?

JavaScript is always pass-by-value. This means everything in JavaScript is a value type and function arguments are always passed by value. That being said, object types are a bit more confusing. The confusion lies in the fact that object types are reference types which are passed by value.

Is JavaScript a reference language?

JavaScript is considered a pass-by-value language, but at the time a variable refers to an object, the value becomes a reference of that object.

Is pass by reference possible in JavaScript?

In JavaScript, that's not possible, so it's not pass-by-reference. It's good that it's possible to pass references to modifiable objects, but that's not what "pass by reference" means.

Is JavaScript pass by value array?

Javascript always pass by value so changing the value of the variable never changes the underlying primitive (String or number). However, when a variable refers to an object which includes array, the value is the reference to the object.


2 Answers

It's interesting in JavaScript. Consider this example:

function changeStuff(a, b, c) {   a = a * 10;   b.item = "changed";   c = {item: "changed"}; }  var num = 10; var obj1 = {item: "unchanged"}; var obj2 = {item: "unchanged"};  changeStuff(num, obj1, obj2);  console.log(num); console.log(obj1.item); console.log(obj2.item);

This produces the output:

10 changed unchanged 
  • If obj1 was not a reference at all, then changing obj1.item would have no effect on the obj1 outside of the function.
  • If the argument was a proper reference, then everything would have changed. num would be 100, and obj2.item would read "changed". Instead, num stays 10 and obj2.item remains "unchanged".

Instead, the situation is that the item passed in is passed by value. But the item that is passed by value is itself a reference. Technically, this is called call-by-sharing.

In practical terms, this means that if you change the parameter itself (as with num and obj2), that won't affect the item that was fed into the parameter. But if you change the internals of the parameter, that will propagate back up (as with obj1).

like image 188
22 revs, 16 users 42% Avatar answered Sep 18 '22 11:09

22 revs, 16 users 42%


It's always pass by value, but for objects the value of the variable is a reference. Because of this, when you pass an object and change its members, those changes persist outside of the function. This makes it look like pass by reference. But if you actually change the value of the object variable you will see that the change does not persist, proving it's really pass by value.

Example:

function changeObject(x) {    x = { member: "bar" };    console.log("in changeObject: " + x.member);  }    function changeMember(x) {    x.member = "bar";    console.log("in changeMember: " + x.member);  }    var x = { member: "foo" };    console.log("before changeObject: " + x.member);  changeObject(x);  console.log("after changeObject: " + x.member); /* change did not persist */    console.log("before changeMember: " + x.member);  changeMember(x);  console.log("after changeMember: " + x.member); /* change persists */

Output:

before changeObject: foo in changeObject: bar after changeObject: foo  before changeMember: foo in changeMember: bar after changeMember: bar 
like image 36
Tim Goodman Avatar answered Sep 22 '22 11:09

Tim Goodman