You could define a getter that acts as a reference to the live variable:

var myArray = Object.defineProperty([0,1], 2, {
    get() { return x },
    enumerable: true,
    configurable: true
});
var x = 2;
console.log(myArray);
x = 3;
console.log(myArray);

Of course, this is a horrible thing to do to an array, and will probably incur heavy performance penalties in every place where the array is used. So: don't do this. Just assign to myArray[2] instead of x in every place, or use a getter/setter function pair instead of the x variable that does this.

function setX(v) { myArray[2] = v; }
var myArray = [0,1,2];
setX(2);
console.log(myArray);
setX(3);
console.log(myArray);
Answer from Bergi on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ jsref โ€บ jsref_obj_array.asp
JavaScript Array Reference
at() charAt() charCodeAt() codePointAt() concat() constructor endsWith() fromCharCode() includes() indexOf() isWellFormed() lastIndexOf() length localeCompare() match() matchAll() padEnd() padStart() prototype repeat() replace() replaceAll() search() slice() split() startsWith() substr() substring() toLocaleLowerCase() toLocaleUpperCase() toLowerCase() toString() toUpperCase() toWellFormed() trim() trimEnd() trimStart() valueOf() JS Typed Arrays ยท Int8Array Uint8Array Uint8ClampedArray Int16Array Uint16Array Int32Array Uint32Array BigInt64Array BigUint64Array Float16Array Float32Array Float64Array JS Typed Reference
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Array
Array - JavaScript | MDN
July 28, 2026 - They do so by first constructing a new array and then populating it with elements. The copy always happens shallowly โ€” the method never copies anything beyond the initially created array. Elements of the original array(s) are copied into the new array as follows: Objects: the object reference is copied into the new array.
Discussions

Object and Arrays - Reference vs Copy
There two thing to consider when creating variable, pass by value and pass by reference Pass by value example: let a = 'thing' let b = a It will produces two completely separate things Pass by reference example: let a = [ 'things' ] let b = a It will produces one thing! It's just have 2 names now, you can call it either a or b further reading More on reddit.com
๐ŸŒ r/learnjavascript
7
40
July 16, 2020
syntax - Reference Array Elements in JavaScript - Stack Overflow
I'm using four-dimensional arrays as sprite sheets in a canvas game, and I want to have a simple way to change key elements the sprites' animations without having to write different setter functions for every sprite. ... Save this answer. ... Show activity on this post. You could define a getter that acts as a reference ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
javascript return reference to array item - Stack Overflow
Because it's an array of Objects (not primitives), the returned value will be a reference to the original object, so modifying a property of the returned object, will update the property of the object that it is referencing. Good article here explaining values and references in Javasript: https://codeburst.io/explaining-value-vs-reference-in-javascript... More on stackoverflow.com
๐ŸŒ stackoverflow.com
javascript - How can I store reference to a variable within an array? - Stack Overflow
I'm trying to create an array that maps strings to variables. It seems that the array stores the current value of the variable instead of storing a reference to the variable. var name = "foo"; var... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ javascript-array-reference
JavaScript Array Reference - GeeksforGeeks
// Create and initialize an array let courses = ["HTML", "CSS", "JavaScript", "React"]; // Display the array items console.log(courses); // Create a new empty array let newArray = []; // forEach loop to push elements // into new array courses.forEach(function (course) { newArray.push(course); }); // Display the new array of items console.log(newArray);
Published: July 23, 2025
๐ŸŒ
Reddit
reddit.com โ€บ r/learnjavascript โ€บ object and arrays - reference vs copy
r/learnjavascript on Reddit: Object and Arrays - Reference vs Copy
July 16, 2020 -

Hi all,

I was reading up on this topic, as I am learning JS and what I took away from it, at least initially is that:

When you reference an array or object, if you create a new variable and make any changes to the property of an object or an item inside an array, not only will that new variable see the changes, but the object or array will also be changed since it is referenced to the original object or array. To avoid this, you need to make a copy of the object or array and then change the value of the property inside of the object or the item within an array. Does this sound correct?

๐ŸŒ
JavaScript.info
javascript.info โ€บ tutorial โ€บ the javascript language โ€บ data types
Arrays
Thatโ€™s because arrays are objects. So both shoppingCart and fruits are the references to the same array.
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Array โ€บ find
Array.prototype.find() - JavaScript | MDN
2 weeks ago - The find() method of Array instances returns the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.
Find elsewhere
๐ŸŒ
W3Schools
www-db.deis.unibo.it โ€บ courses โ€บ TW โ€บ DOCS โ€บ w3schools โ€บ jsref โ€บ jsref_obj_array.asp.html
JavaScript Array Reference
For a tutorial about Arrays, read our JavaScript Array Tutorial. ... Color Converter Google Maps Animated Buttons Modal Boxes Modal Images Tooltips Loaders JS Animations Progress Bars Dropdowns Slideshow Side Navigation HTML Includes Color Palettes Code Coloring ... Your message has been sent to W3Schools. HTML Tutorial CSS Tutorial JavaScript Tutorial W3.CSS Tutorial Bootstrap Tutorial SQL Tutorial PHP Tutorial jQuery Tutorial Angular Tutorial XML Tutorial ยท HTML Reference CSS Reference JavaScript Reference W3.CSS Reference Browser Statistics PHP Reference HTML Colors HTML Character Sets jQuery Reference AngularJS Reference
๐ŸŒ
Treehouse Blog
blog.teamtreehouse.com โ€บ javascript-basic-array-methods
JavaScript Basic Array Methods | Treehouse Blog
September 11, 2023 - MDN โ€“ Arrays https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array MDN โ€“ .length() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length MDN โ€“ push() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push MDN โ€“ .pop() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop MDN โ€“ .shift() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift MDN โ€“ .unshift() https://developer.mozilla.org/en-US/docs/Web
๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ js_array_methods.asp
JavaScript Array Methods
This is not possible in JavaScript, because [] is used for accessing both arrays and objects. obj[-1] refers to the value of key -1, not to the last property of the object.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ javascript-array-methods
JavaScript Array Methods - GeeksforGeeks
The reverse() method is used to reverse the order of elements in an array. It modifies the array in place and returns a reference to the same array with the reversed order.
Published: June 1, 2026
Top answer
1 of 7
68

I tried to use Array.filter() function, but it returns a new array instead of a reference to the original array. which I can not use to mutate the original array.

It returns a new array, but the array's entries are still references to the same objects. So filter is just fine for this.

var filteredUsers = users.filter(function(entry) { return entry.id === 2; });
var item = filteredUsers[0];
item.name = "user2 name updated";
console.log(users[1].name) // "user2 name updated"

The array contains references to the objects, not copies of them. When we do item = users[1], or use filter to get a new array containing a subset of the objects, we get a copy of the object reference, and so the variable now refers to the same object, like this:

                +โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’+
usersโˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’>| (array)  |          
                +โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’+           +โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’+
                |   0      |โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’>|    (object)   |
                |          |           +โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’+
                |          |           | id:   1       |
                |          |           | name: "name1" |
                |          |           +โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’+
                |   1      |โˆ’โˆ’+
                |          |  |
                |          |  |        +โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’+
                |          |  +โˆ’โˆ’โˆ’+โˆ’+โˆ’>|    (object)   |
                +โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’+     / /   +โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’+
                                 | |   | id:   2       |
                +โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’+     | |   | name: "name2" |
filteredUsersโˆ’โˆ’>| (array)  |     | |   +โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’+
                +โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’+     | |   
                |   0      |โˆ’โˆ’โˆ’โˆ’โˆ’+ |
                +โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’+       |   
                                   |
itemโˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’+

Changing the object changes the object, and those changes are visible regardless of which reference to the object you use to look at its properties.

Now if you had an array of primitives:

var a = [1, 2, 3, 4, 5];

...then of course, you can't use the approach above because they aren't objects. In that case, you'd use indexOf to find the index:

var index = a.indexOf(3); // Find the first entry === 3

...and then modify the entry

a[index] = 42;

This also applies to strings (provided they're nice normal string primitives, not things you created via new String(), which there's virtually never a reason to do).

2 of 7
44

To do it similarly to the accepted answer, but a bit more succinctly, let us use Array.find, like so:

var item = users.find(u => u.id === 2)
item.name = "user2 name updated"
console.log(users[1].name) // "user2 name updated"

Explanations are very similar to what has been provided in the accepted answer.

๐ŸŒ
Medium
medium.com โ€บ @gmlearnshealth โ€บ understanding-javascript-array-reference-types-and-equality-e1bad3db512b
Understanding JavaScript Array Reference Types and Equality | by Guo Ming | Medium
May 8, 2025 - JavaScript arrays are reference types, which means they are stored in memory differently from primitive types (like numbers or strings).
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Array โ€บ some
Array.prototype.some() - JavaScript | MDN
The array argument is useful if you want to access another element in the array, especially when you don't have an existing variable that refers to the array.
๐ŸŒ
BrainStationยฎ
brainstation.io โ€บ learn โ€บ javascript โ€บ array
JavaScript Array (2026 Tutorial & Examples) | BrainStationยฎ
February 4, 2025 - JavaScript arrays are collections of items. These items can be of the same type or different but arrays provide a way to group them together into a common collection. Since a JavaScript array is a collection, it is stored and used differently ...
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Learn_web_development โ€บ Core โ€บ Scripting โ€บ Arrays
Arrays - Learn web development | MDN
After reading through this article, we are sure you will agree that arrays seem pretty darn useful; you'll see them crop up everywhere in JavaScript, often in association with loops in order to do the same thing to every item in an array. We'll be teaching you all about loops later on in the module. In the next article, we'll give you some tests that you can use to check how well you've understood and retained the information we've provided on arrays. ... The Array object reference page provides a detailed reference guide to the features discussed in this page, and many other Array features.
๐ŸŒ
HCL Software
help.hcl-software.com โ€บ dom_designer โ€บ 9.0.1 โ€บ reference โ€บ r_wpdr_standard_array_array_r.html
Array (Array - JavaScript)
var a = new Array(new Array(3), new Array(3), new Array(3)) Make reference to an array element with a subscript in square brackets. The first element of an array is element 0. For example, if a is an array, a[0] is the first element, a[1] is the second element, and so on.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ javascript-arrays
JavaScript Arrays - GeeksforGeeks
Arrays can hold any type of data-such as numbers, strings, objects, or even other arrays-making them a flexible and essential part of JavaScript programming.
Published: 3 weeks ago