There is no "pass by reference" available in JavaScript. You can pass an object (which is to say, you can pass-by-value a reference to an object) and then have a function modify the object contents:

function alterObject(obj) {
  obj.foo = "goodbye";
}

var myObj = { foo: "hello world" };

alterObject(myObj);

alert(myObj.foo); // "goodbye" instead of "hello world"

You can iterate over the properties of an array with a numeric index and modify each cell of the array, if you want.

var arr = [1, 2, 3];

for (var i = 0; i < arr.length; i++) { 
    arr[i] = arr[i] + 1; 
}

It's important to note that "pass-by-reference" is a very specific term. It does not mean simply that it's possible to pass a reference to a modifiable object. Instead, it means that it's possible to pass a simple variable in such a way as to allow a function to modify that value in the calling context. So:

 function swap(a, b) {
   var tmp = a;
   a = b;
   b = tmp; //assign tmp to b
 }

 var x = 1, y = 2;
 swap(x, y);

 alert("x is " + x + ", y is " + y); // "x is 1, y is 2"

In a language like C++, it's possible to do that because that language does (sort-of) have pass-by-reference.

edit โ€” this recently (March 2015) blew up on Reddit again over a blog post similar to mine mentioned below, though in this case about Java. It occurred to me while reading the back-and-forth in the Reddit comments that a big part of the confusion stems from the unfortunate collision involving the word "reference". The terminology "pass by reference" and "pass by value" predates the concept of having "objects" to work with in programming languages. It's really not about objects at all; it's about function parameters, and specifically how function parameters are "connected" (or not) to the calling environment. In particular, note that in a true pass-by-reference language โ€” one that does involve objects โ€” one would still have the ability to modify object contents, and it would look pretty much exactly like it does in JavaScript. However, one would also be able to modify the object reference in the calling environment, and that's the key thing that you can't do in JavaScript. A pass-by-reference language would pass not the reference itself, but a reference to the reference.

edit โ€” here is a blog post on the topic. (Note the comment to that post that explains that C++ doesn't really have pass-by-reference. That is true. What C++ does have, however, is the ability to create references to plain variables, either explicitly at the point of function invocation to create a pointer, or implicitly when calling functions whose argument type signature calls for that to be done. Those are the key things JavaScript doesn't support.)

Answer from Pointy on Stack Overflow
Top answer
1 of 16
551

There is no "pass by reference" available in JavaScript. You can pass an object (which is to say, you can pass-by-value a reference to an object) and then have a function modify the object contents:

function alterObject(obj) {
  obj.foo = "goodbye";
}

var myObj = { foo: "hello world" };

alterObject(myObj);

alert(myObj.foo); // "goodbye" instead of "hello world"

You can iterate over the properties of an array with a numeric index and modify each cell of the array, if you want.

var arr = [1, 2, 3];

for (var i = 0; i < arr.length; i++) { 
    arr[i] = arr[i] + 1; 
}

It's important to note that "pass-by-reference" is a very specific term. It does not mean simply that it's possible to pass a reference to a modifiable object. Instead, it means that it's possible to pass a simple variable in such a way as to allow a function to modify that value in the calling context. So:

 function swap(a, b) {
   var tmp = a;
   a = b;
   b = tmp; //assign tmp to b
 }

 var x = 1, y = 2;
 swap(x, y);

 alert("x is " + x + ", y is " + y); // "x is 1, y is 2"

In a language like C++, it's possible to do that because that language does (sort-of) have pass-by-reference.

edit โ€” this recently (March 2015) blew up on Reddit again over a blog post similar to mine mentioned below, though in this case about Java. It occurred to me while reading the back-and-forth in the Reddit comments that a big part of the confusion stems from the unfortunate collision involving the word "reference". The terminology "pass by reference" and "pass by value" predates the concept of having "objects" to work with in programming languages. It's really not about objects at all; it's about function parameters, and specifically how function parameters are "connected" (or not) to the calling environment. In particular, note that in a true pass-by-reference language โ€” one that does involve objects โ€” one would still have the ability to modify object contents, and it would look pretty much exactly like it does in JavaScript. However, one would also be able to modify the object reference in the calling environment, and that's the key thing that you can't do in JavaScript. A pass-by-reference language would pass not the reference itself, but a reference to the reference.

edit โ€” here is a blog post on the topic. (Note the comment to that post that explains that C++ doesn't really have pass-by-reference. That is true. What C++ does have, however, is the ability to create references to plain variables, either explicitly at the point of function invocation to create a pointer, or implicitly when calling functions whose argument type signature calls for that to be done. Those are the key things JavaScript doesn't support.)

2 of 16
198
  1. Primitive type variables like strings and numbers are always passed by value.
  2. Arrays and Objects are passed by reference or by value based on these conditions:
  • if you are setting the value of an object or array it is Pass by Value.

     object1 = { prop: "car" };
     array1 = [1,2,3];
    
  • if you are changing a property value of an object or array then it is Pass by Reference.

     object1.prop = "car";
     array1[0] = 9;
    

Code

function passVar(obj1, obj2, num) {
    obj1.prop = "laptop"; // will CHANGE original
    obj2 = { prop: "computer" }; //will NOT affect original
    num = num + 1; // will NOT affect original
}

var object1 = {
    prop: "car"
};
var object2 = {
    prop: "bike"
};
var number1 = 10;

passVar(object1, object2, number1);
console.log(object1); // output: Object { prop: "laptop" }
console.log(object2); // output: Object { prop: "bike" }
console.log(number1); // ouput: 10

๐ŸŒ
Reddit
reddit.com โ€บ r/javascript โ€บ is javascript pass by reference?
r/javascript on Reddit: Is JavaScript Pass by Reference?
April 17, 2023 - My simple way to know: If you can write a proper swap-function (swap(a, b)), then the language supports "pass by reference", if not, then everything is "pass by value". In languages like Java and Javascript, you cannot write a proper swap-function, which means the language does not support "pass by reference". Really wish people would stop saying that you pass objects and arrays "by reference", as it is misleading.
Discussions

Does Javascript pass array to function by reference or value? - Stack Overflow
I also know that the slice(0) function just return a shallow copy of array, but in case the array is only a array of integer. So my question is why the data of aiTest is not modified? ... Duplicate. Short answer: Javascript is always pass by value, EXCEPT for arrays/objects. ... JavaScript is always by-value (copy). Though, with objects, the value is a reference ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Passing an array as a function parameter in JavaScript - Stack Overflow
As a side note, if anyone wants to pass an associative array (named keys) instead, then use an object. Coming from PHP (and always led to this thread by google) this took me a while to figure out. You can pass the whole object as a parameter then. w3schools.com/js/js_objects.asp 2014-05-29T08:56:49.823Z+00:00 ... Thanks for pointing out the 'spread' argument! Didn't know about it. 2017-11-12T21:44:51.36Z+00:00 ... @timhc - your side note comment is intriguing, but I can't parse it (I'm a javascript ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Passing JavaScript arrays to functions - Stack Overflow
What error? This is a completely valid syntax in JavaScript. ... Arrays in JS are objects and are passed into functions by value, where that value is a reference to the array. More on stackoverflow.com
๐ŸŒ stackoverflow.com
Is it possible in Javascript to ACTUALLY pass an array as reference? - Stack Overflow
Is it possible in Javascript to pass an array (from outer scope) to a function, and mutate the original array? I've read several answers mentioning that f()'s arr argument is actually a copy, it's name just shadows the outer scope variable. Other mention arrays are always passed by reference, but ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
AlmaBetter
almabetter.com โ€บ bytes โ€บ tutorials โ€บ javascript โ€บ javascript-pass-by-reference-or-value
Javascript Pass by Reference or Value
April 25, 2024 - As expected, the value of num is still 5 because the double function did not modify it. Instead, the function created a copy of the value of num, doubled it, and returned the new value. In JavaScript, objects and arrays are passed by reference.
๐ŸŒ
Scientech Easy
scientecheasy.com โ€บ home โ€บ blog โ€บ passing array to function in javascript
Passing Array to Function in JavaScript - Scientech Easy
3 weeks ago - To pass an array argument to a function, simply pass the name of an array (a reference to an array) without brackets. For example, if we have declared an array marks as: ... passes array hourlyTemp to the function modifyArray(). JavaScript automatically passes arrays to functions using ...
๐ŸŒ
DEV Community
dev.to โ€บ bytebodger โ€บ a-gotcha-of-javascript-s-pass-by-reference-1afm
A "Gotcha" of JavaScript's Pass-by-Reference - DEV Community
August 24, 2020 - If objects (and arrays are objects) are passed-by-reference, then, someArray will be a reference to originalArray. And if that were true, then when we set someArray = [1], that change should be reflected in originalArray, outside the function scope.
๐ŸŒ
Medium
medium.com โ€บ nodesimplified โ€บ javascript-pass-by-value-and-pass-by-reference-in-javascript-fcf10305aa9c
[Javascript] Pass By Value And Pass By Reference In JavaScript | by Raghuraman Kesavan | Bondesk.In | Medium
April 21, 2020 - function callByReference(varObj) { console.log("Inside Call by Reference Method"); varObj.a = 100; console.log(varObj); } let varObj = {a:1};console.log("Before Call by Reference Method"); console.log(varObj);callByReference(varObj) console.log("After Call by Reference Method"); console.log(varObj);output will be : --------------- Before Call by Reference Method {a: 1} Inside Call by Reference Method {a: 100} After Call by Reference Method {a: 100} so if we are passing object or array as an argument to the method, then there is a possibility that value of the object can change.
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ pass-by-value-and-pass-by-reference-in-javascript
Pass by Value and Pass by Reference in Javascript - GeeksforGeeks
July 23, 2025 - Instead: Primitive types (like numbers, strings, booleans) are always passed by valueโ€”a copy of the value is passed. For objects and arrays, JavaScript passes the reference value, which is essentially a pointer to the memory location of the data.
๐ŸŒ
Flexiple
flexiple.com โ€บ javascript โ€บ javascript-pass-by-reference-or-value
Javascript pass by reference or value - Flexiple
In Pass by Reference, a function is called by directly passing the reference/address of the variable as the argument. Changing the argument inside the function affects the variable passed from outside the function.
๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ js_function_parameters.asp
JavaScript Function Parameters
JSON Intro JSON Syntax JSON vs XML JSON Data Types JSON Parse JSON Stringify JSON Objects JSON Arrays JSON Server JSON PHP JSON HTML JSON JSONP JS jQuery ยท jQuery Selectors jQuery HTML jQuery CSS jQuery DOM JS Graphics ยท JS Graphics JS Canvas JS Plotly JS Chart.js JS Google Chart JS D3.js JS Examples ยท JS Examples JS HTML DOM JS HTML Input JS HTML Objects JS HTML Events JS Browser JS Editor JS Exercises JS Quiz JS Website JS Syllabus JS Study Plan JS Interview Prep JS Bootcamp JS Certificate JS Reference ... Parameters allow you to pass (send) values to a function.
๐ŸŒ
TutorialsTonight
tutorialstonight.com โ€บ javascript-pass-array-to-function
JavaScript Pass Array to Function (in 3 ways)
In such cases knowing multiple ways to pass arrays to functions can be very helpful. You now understand how to pass an array to a function by reference, using the spread operator, and using the apply() method.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ javascript โ€บ pass-by value and pass-by reference in javascript
Pass-By Value and Pass-By Reference in JavaScript with Examples - Scaler Topics
June 23, 2024 - This example states that, like objects, in arrays also, on changing the value of tempArr, the value of originalArr changes automatically. Thus we can conclude by saying all the non-primitive data types interact by the reference, so when we set their values equal to each other or pass them to a function, then they all point to the same memory space (address) whenever we change one of the value, then all of the values get changes. As in pass-by value in JavaScript, a new copy of the variable is created, and any changes made in the new variable are independent of the original variable, so it is useful when we want to keep track of the initial variable and don't want to lose its value.
๐ŸŒ
Medium
medium.com โ€บ @sayes2x โ€บ passing-an-array-to-a-function-as-a-value-in-javascript-10c20d5b89
Passing an Array to a Function as a Value in JavaScript | by Scott Price | Medium
October 5, 2018 - When you pass an array into a function in JavaScript, it is passed as a reference. Anything you do that alters the array inside the function will also alter the original array.
๐ŸŒ
JavaScript Tutorial
javascripttutorial.net โ€บ home โ€บ javascript tutorial โ€บ javascript pass-by-value
JavaScript Pass By Value
November 15, 2024 - When passing the variable y to the square() function, JavaScript copies y value to the x variable. After that, the square() function changes the x variable. However, it does not impact the value of the y variable because x and y are separate variables. Finally, the value of the y variable does not change after the square() function completes. If JavaScript used the pass-by-reference, the variable y would change to 100 after calling the function.
๐ŸŒ
Medium
mayuminishimoto.medium.com โ€บ javascript-pass-by-value-and-pass-by-reference-f9b80d2221b8
JavaScript: Pass-by-value and Pass-by-reference | by Mayumi Nishimoto | Medium
June 7, 2020 - In essence, when a parameter is passed-by-reference both variables โ€” the variable we pass into the function as an argument โ€” in the caller โ€” and the local variable created with the same parameterโ€™s name โ€” in the callee โ€” will point to the same address in computer memory. As a result, all changes done by the callee will mutate the original object and all the variables that have reference to that object will see the change. On line 1 of the example below, shoppingListis initialised and assigned an array with four elements of type string.