4

I can't figure out what passing by reference and passing by value looks like (I know what they mean in theory). Can you tell me whether the below function is passing arguments by value ore reference? BTW this is my first post :)

var number_1 = 32;
var number_2 = 43;

function addition(num1,num2) {
    alert (num1*num2) ;
}

addition(number_1,number_2); /*is this passing by reference?*/
  • 2
    Technically everything in JS is by value, though for objects the value is a reference to the object. In your case, it's a simple copy of the number values. – cookie monster Aug 13 '14 at 01:42
  • 1
    everything in javascript is passed by value – Prabhu Murthy Aug 13 '14 at 01:42
  • 1
    Passing by reference is largely a concept in other languages, not Javascript. In Javascript, every variable is either a primitive type (numeric, boolean, string), or an object. Arrays are just objects that are built-in to the language and have special syntax. – Shashank Aug 13 '14 at 02:22
  • can someone give me two simple examples of passing arguments by reference and by value side by side...I know that Javascript only allows passing by value but I can't understand how it works in my code. Doesn't passing an argument by reference mean passing a variable instead of a raw value? – user3918807 Aug 13 '14 at 16:04

2 Answers2

5

Primitive types

In javascript number, strings, boolean fall under the category of primitives. And whenever such types are passed as arguments to a function, a separate copy is created inside the function scope which has no impact on the outer scope

for e.g

var number_1 = 32;
var number_2 = 43;
addition(32,43);
//number_1 is 32 here
//number_2 is 43 here

function addition(number_1,number_2) {
    number_1 += 1; // number_1 is 33
    number_2 += 1; // number_2 is 44
}

Reference types

Reference types are slightly different

Take the following example

var obj = new Object();
fn(obj);

function fn(object){
  object.property = "test";
  //obj.property is "test"
  object = new Object();
  object.property = "test 2";
  //obj.property is still "test"
  //obj.property should have changed to "test 2", 
  //if it had been passed by reference
}

if it had been passed by reference , obj.property should have got changed to "test 2" after the last statement inside fn, but it didnt. so when passing reference values to functions, a separate copy of the pointer to the object is passed.

Prabhu Murthy
  • 9,031
  • 5
  • 29
  • 36
  • thanks, so basically what I did was pass by value then? I thought that passing by value meant passing a raw value like "thisisastring" and that passing a variable like number_1 meant passing by reference. – user3918807 Aug 13 '14 at 16:17
  • yes correct. you were passing by value and that is how it works – Prabhu Murthy Aug 13 '14 at 16:19
-1

In javascript every variable is passed by value not reference but there is an exception for objects. objects are references (like it was answered here).

which means a number, a string, a boolean or a function don't change at all after you put'em as argument in a function. but an the propertues and the elements inside an object or an array change if they are changed inside a function.

Here is an example


Simple variable

function nullify(a) 
{
   a = null;
}

var myVariable = 123;
nullify(myVariable); // myVariable still equals to 123

Object

function changeProperty (obj)
{
   obj.key = null;
}

var myObject = new Object();
myObject.key = "value";
changeProperty(myObject); // the property key is null after calling the function

Array

function changeArray (Arr)
{
   Arr[0] = null;
}

var myArray = [1,2,3];
changeArray(myArray); // now the value of myArray is [null,2,3]
Community
  • 1
  • 1
Khalid
  • 4,730
  • 5
  • 27
  • 50
  • Objects aren’t *passed* by reference; objects are references. Same as assignment. – Ry- Aug 13 '14 at 02:19
  • @minitech I didn't say they are passed by reference – Khalid Aug 13 '14 at 02:21
  • To quote from your first paragraph: *“In javascript every variable is passed by value not reference **except for objects**.”* – Ry- Aug 13 '14 at 02:21
  • I said something and you understood something else! – Khalid Aug 13 '14 at 02:24
  • Please clarify, then. If objects aren’t passed by value and aren’t passed by reference, how are they passed? – Ry- Aug 13 '14 at 02:25
  • @minitech, check out the new update, I hope you will understand :) have a good day! – Khalid Aug 13 '14 at 02:31
  • Okay, but that’s not an exception. Changing a property is completely different from assigning a new value to a variable. Primitives are immutable. – Ry- Aug 13 '14 at 02:32
  • There's always an (Add Another Answer) button at the buttom of the page – Khalid Aug 13 '14 at 02:35
  • No, your answer contains a link to the duplicate anyways. – Ry- Aug 13 '14 at 02:39
  • If BOSS has the right answer then your comments are redundant – Khalid Aug 13 '14 at 02:39
  • Not really. You could fix your answer or something, by the way. – Ry- Aug 13 '14 at 02:40
  • No need to fix it, I think it's clear for most of the people who would read it – Khalid Aug 13 '14 at 02:48
  • Yes, I believe it will give them a very clear picture that is wrong. – Ry- Aug 13 '14 at 02:50
  • if you think that objects and arrays are assigned new values then why an object doesn't change in this case : `var x = {}; nullify(x);` – Khalid Aug 13 '14 at 02:53
  • I don’t think that at all. – Ry- Aug 13 '14 at 02:54
  • Here's what you've said **Changing a property is completely different from assigning a new value to a variable** – Khalid Aug 13 '14 at 02:56
  • It is completely different from assigning a new value to a variable. What I said is exactly the opposite of that `var x = {}; nullify(x);` example. – Ry- Aug 13 '14 at 02:58
  • 1
    Serially downvoting me is not creative at all. Also, imagine how you could have spent that time just fixing your answer! – Ry- Aug 13 '14 at 03:02