🌐
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 Web Docs
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
Referencing Arrays in Javascript - Stack Overflow
Arrays are passed by reference in JavaScript. More on stackoverflow.com
🌐 stackoverflow.com
🌐
Tutorial Republic
tutorialrepublic.com β€Ί javascript-reference β€Ί javascript-array-object.php
JavaScript Array Properties and Methods - Tutorial Republic
This chapter contains a brief overview of the properties and method of the global array object. The JavaScript Array object is a global object that is used in the construction of arrays.
🌐
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?

🌐
MDN Web Docs
developer.mozilla.org β€Ί en-US β€Ί docs β€Ί Web β€Ί JavaScript β€Ί Reference β€Ί Global_Objects β€Ί Array β€Ί find
Array.prototype.find() - JavaScript - MDN Web Docs
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.
🌐
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.
Find elsewhere
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.

🌐
DEV Community
dev.to β€Ί simo_benhida β€Ί reference-and-copying-objetcs--arrays-in-javascript-2h23
Reference And Copying Objects & Arrays in Javascript - DEV Community
December 30, 2019 - array1[2] = "David" console.log(array1) //["John", "Doe", "David"] console.log(array) //["John", "Doe", "David"]
Top answer
1 of 4
4

It looks like you are confusing JavaScript array objects with the fact that all objects are associative arrays. Note that normal objects (e.g. the "Fruit" object you allude to) do not have an intrinsic ordering of properties (keys) whereas Array objects do (due to the natural ordering of integral indices). Essentially, an Array is just an object with a special "length" property that stores the last integer index (starting from zero) plus one.

Any object's properties will be iterated in arbitrary (e.g. random) order:

var obj = {a:'Aye', b:'Bee', c:'See', d:'Dee'};
for (var prop in obj) {
  alert(prop + '=' + obj[prop]); // No order is guaranteed.
}

Strictly speaking, even arrays are not guaranteed by the specification to iterate in natural order using a for-in loop; however, most JavaScript interpreters do so anyway. (Notably, IE8 iterates in the order that indices were assigned, not natural order.) This is why you should always iterate arrays using indices in a plain "for" loop.

var arr = ['a', 'b', 'c', 'd', 'e', 'f'];
for (var i=0; i<arr.length; i++) { // i = [0 .. length-1]
  alert(arr + '=' + arr[i]); // Explicitly order as 'a', 'b', 'c'...
}

These differences mean that regardless of how your "Fruit" object is defined there is no reliable way to ensure a strict ordering of keys (e.g. "Banana", "Apple", "Color", "Size", etc.) unless you retain your own ordering separately.

2 of 4
3

I'm a little confused, because you seem to have answered your own question. I also don't know how you have your array(s) set up. But if you take a look at this example, you can see it working here:

http://jsfiddle.net/uyNnH/

This assumes that the top level array contains your fruit types and second level array contains each fruit type's properties.

Update:

From Mozilla and others:

In JavaScript 1.0, you can refer to an object's properties by their property name or by their ordinal index. In JavaScript 1.1 or later, however, if you initially define a property by its name, you must always refer to it by its name, and if you initially define a property by an index, you must always refer to it by its index.

So the issue is that you declare an associative array, instead of an indexed/ordinal-based one. The only solution I can think of is using a (ugly) for loop. Here's some un-tested pseudo-code:

function getItemByIndex(index, array) {
    var counter = 0;

    for (item in array)
    {
        if (counter == index)
        {
            return item;
        }

        counter++;
    }
}
🌐
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).
🌐
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
🌐
MDN Web Docs
developer.mozilla.org β€Ί en-US β€Ί docs β€Ί Web β€Ί JavaScript β€Ί Reference β€Ί Global_Objects β€Ί Array β€Ί some
Array.prototype.some() - JavaScript - MDN Web Docs
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.
🌐
CodeBurst
codeburst.io β€Ί explaining-value-vs-reference-in-javascript-647a975e12a0
Explaining Value vs. Reference in Javascript | by Arnav Aggarwal | codeburst
August 17, 2017 - When we assign and use a reference-type variable, what we write and see is: ... Notice that the value, the address, contained by the variable arr is static. The array in memory is what changes. When we use arr to do something, such as pushing a value, the Javascript engine goes to the location of arr in memory and works with the information stored there.