Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 613
Anil Namde Avatar asked May 14 '10 14:05

Anil Namde


People also ask

Is there pass by reference in JavaScript?

In JavaScript array and Object follows pass by reference property. In Pass by reference, parameters passed as an arguments does not create its own copy, it refers to the original value so changes made inside function affect the original value. let us take an example to understand better.

Does JavaScript always pass parameters by value or by reference?

In JavaScript, all function arguments are always passed by value. It means that JavaScript copies the values of the variables into the function arguments. Any changes that you make to the arguments inside the function do not reflect the passing variables outside of the function.

Are objects passed by value in JavaScript?

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.

Are primitives passed by value in JavaScript?

The simple rule of passing by value is that all primitive values in JavaScript are passed by value. Simple as that. Passing by value means that every time you assign a value to a variable, a copy of that value is created.


2 Answers

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!

like image 174
jimbo Avatar answered Oct 08 '22 11:10

jimbo


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}
like image 44
Andy E Avatar answered Oct 08 '22 11:10

Andy E