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

🌐
Medium
medium.com › @naveenkarippai › learning-how-references-work-in-javascript-a066a4e15600
Learning how references work in JavaScript | by Naveen Karippai | Medium
January 14, 2023 - TL;DR: There are NO pointers in JavaScript and references work differently from what we would normally see in most other popular programming languages. In JavaScript, it’s just NOT possible to have a reference from one variable to another variable.
🌐
W3Schools
w3schools.com › js › js_variables.asp
JavaScript Variables
JS Examples JS HTML DOM JS HTML ... Interview Prep JS Bootcamp JS Certificate JS Reference ... JavaScript variables are containers for data....
🌐
Reddit
reddit.com › r/javascript › is javascript pass by reference?
r/javascript on Reddit: Is JavaScript Pass by Reference?
April 17, 2023 - That's why the conclusion was that, in JavaScript, we're passing references around, but we pass those references "by values". Here, were using two different definitions for "reference" in one sentence, which is what makes this Uber confusing. Yes everything is a reference type (definition 1), but those references (definition 1) are not passed by reference (definition 2), but by value, i.e. we're not having two names for the same variable (which is pass by reference definition 2), but we are copying the the reference's (definition 1) address, hence it's pass by value.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › reference-and-copy-variables-in-javascript
Reference and Copy Variables in JavaScript - GeeksforGeeks
Reason: Primitive values are copied by value in JavaScript. ... let name = 'sam'; let name2 = name; document.write(name, name2); document.write("<br>"); name = 'xyz'; document.write(name, name2); ... Both print as sam sam. ... const players = ['Sam', 'Sarah', 'Ryan', 'Poppy']; const team = players; document.write(players, team); ... Both variables reference the same array in memory.
Published   January 22, 2026
🌐
AlmaBetter
almabetter.com › bytes › tutorials › javascript › javascript-pass-by-reference-or-value
Javascript Pass by Reference or Value
April 25, 2024 - When a reference type is passed as an argument to a function, a reference to the original value is passed to the function, rather than a copy of the value. This means that changes made to the value within the function affect the original value. When you pass a variable of a primitive data type as an argument to a function, a copy of the variable's value is created and passed to the function.
Find elsewhere
🌐
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 - Once someone gets it in their head that "JavaScript has no pass-by-reference!!!" it becomes nearly impossible to dislodge their erroneous conclusion. If you've been writing code for, oh... five minutes or so, nothing in this next example will surprise you. Nevertheless, it's important to illustrate the extremely simple concept at play: // initialize our variables let mostImportantNumber = 3.14; let spanishNumbers = { one: 'uno', two: 'dos', three: 'tres' }; // use these variables to initialize some NEW variables let answerToEverything = mostImportantNumber; let germanNumbers = spanishNumbers;
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Statements › var
var - JavaScript | MDN
At the time x = y is evaluated, y exists so no ReferenceError is thrown and its value is undefined. So, x is assigned the undefined value. Then, y is assigned the value "A". Be careful of the var x = y = 1 syntax — y is not actually declared as a variable, so y = 1 is an unqualified identifier assignment, which creates a global variable in non-strict mode.
🌐
DEV Community
dev.to › muhammedshameel › understanding-reference-vs-value-in-javascript-ab4
Understanding Reference vs. Value in JavaScript - DEV Community
October 9, 2023 - Reference types store a reference to the object, and changes to one variable will affect others referencing the same object. Mastering this concept will help you write more predictable and bug-free JavaScript code in your projects.
🌐
Flexiple
flexiple.com › javascript › javascript-pass-by-reference-or-value
Javascript pass by reference or value - Flexiple
March 14, 2022 - Let us understand what pass by reference and pass by value is before looking at examples. In Pass by Reference, a function is called by directly passing the reference/address of the variable as the argument.
🌐
SitePoint
sitepoint.com › blog › javascript › quick tip: how javascript references work
Quick Tip: How JavaScript References Work — SitePoint
November 6, 2024 - TL;DR: There are NO pointers in JavaScript and references work differently from what we would normally see in most other popular programming languages. In JavaScript, it’s just NOT possible to have a reference from one variable to another variable. And, only compound values (e.g..
🌐
freeCodeCamp
freecodecamp.org › news › javascript-assigning-values-vs-assigning-references
JavaScript Primitive Values vs Reference Values – Explained with Examples
September 11, 2024 - In summary, understanding the difference between values and references in JavaScript is essential for writing efficient and bug-free code. By being aware of how data is stored and manipulated, you can avoid unexpected behavior and improve the performance of your applications. Remember that primitive types are passed by value, while objects and arrays are passed by reference. Keep this in mind when working with functions and assigning variables.
🌐
Stackademic
blog.stackademic.com › reference-variables-in-javascript-ab9ed3efb129
Reference Variables in JavaScript | by Abhishek sojitra | Stackademic
October 3, 2023 - In JavaScript, reference variables, often referred to as variables that store references, work by pointing to objects or values in memory.
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › objects: the basics
Object references and copying
October 1, 2022 - To handle such complex cases we ... and copied by reference. In other words, a variable stores not the “object value”, but a “reference” (address in memory) for the value....
🌐
Codedamn
codedamn.com › news › javascript
Pass by Reference in JavaScript – Everything you need to know
July 2, 2023 - On the other hand, "Pass by Reference" means that a reference to the original variable is passed to the function. Any changes made to this reference will also affect the original variable. JavaScript, interestingly, does not have typical pass by reference like other languages such as C++ or Java.
🌐
Medium
medium.com › front-end-weekly › understanding-pass-by-value-and-pass-by-reference-in-javascript-8e2a0806b175
Understanding Pass by Value and Pass by Reference in JavaScript | by Ali Husen | Frontend Weekly | Medium
July 8, 2023 - We then declare another variable b and set it equal to a. At this point, both a and b are 10. However, when we change the value of a to 20, b remains 10 because the value was passed by value - meaning the value 10 was copied to b when it was declared, and changes to a do not affect b. While JavaScript is primarily a “pass by value” language, it uses a concept called “pass by reference” when dealing with objects (including arrays and functions).
🌐
W3Schools
w3schools.com › js › js_function_parameters.asp
JavaScript Function Parameters
Temporal Study Path Temporal Intro Temporal vs Date Temporal Duration Temporal Instant Temporal PlainDateTime Temporal PlainDate Temporal PlainYearMonth Temporal PlainMonthDay Temporal PlainTime Temporal ZonedDateTime Temporal Now Temporal Add/Subtract Temporal Since/Until Temporal Compare Temporal Conversion Temporal Formats Temporal Mistakes Temporal Migrate Temporal Standards Temporal Reference
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Learn_web_development › Core › Scripting › Variables
Storing the information you need — Variables - Learn web development | MDN
Any time they see this name, they will know what it refers to. In this course, we adopt the following principle about when to use let and when to use const: Use const when you can, and use let when you have to. This means that if you can initialize a variable when you declare it, and don't need to reassign it later, make it a constant. By now you should know a reasonable amount about JavaScript variables and how to create them.
🌐
Devmio
devm.io › javascript › by-value-or-by-reference-how-javascript-handles-your-data
By Value or By Reference? How JavaScript Handles Your Data
The distinction between passing by value and passing by reference in JavaScript is one of the most misunderstood concepts, often leading to subtle bugs and unexpected behaviors in applications. In this article, we will provide a clear and in-depth explanation of how primitive types, objects, ...
🌐
Quora
quora.com › Why-does-JavaScript-not-support-pass-by-reference-How-do-functions-in-JavaScript-change-passed-parameters-values-without-returning-anything
Why does JavaScript not support pass by reference? How do functions in JavaScript change passed parameters' values without returning anything? - Quora
In javascript, integers, strings and booleans are passed by value, and arrays and objects are passed by reference. This means that when we pass an integer, string or boolean to a function, javascript makes a temporary copy of that argument.