Why does changing an Array in JavaScript affect copies of the array?

I’ve written the following JavaScript:

var myArray = ['a', 'b', 'c'];
var copyOfMyArray = myArray;
copyOfMyArray.splice(0, 1);
alert(myArray); // alerts ['b','c']
alert(copyOfMyArray); // alerts ['b','c']

var myNumber = 5;
var copyOfMyNumber = myNumber;
copyOfMyNumber = copyOfMyNumber - 1;
alert(myNumber); // alerts 5
alert(copyOfMyNumber); // alerts 4        

This code declares a variable myArray and sets it to an array value. It then declares a second variable copyOfMyArray and sets it to myArray. It performs an operation on copyOfMyArray and then alerts both myArray and copyOfMyArray. Somehow, when I perform an operation on copyOfMyArray, it appears that the same operation is performed on myArray.

The code then does the same thing with a number value: It declares a variable myNumber and sets it to a number value. It then declares a second variable copyOfMyNumber and sets it to myNumber. It performs an operation on copyOfMyNumber and then alerts both myNumber and copyOfMyNumber. Here, I get the expected behavior: different values for myNumber and copyOfMyNumber.

What is the difference between an array and a number in JavaScript that it seems changing an array changes the value of a copy of the array, where as changing a number does not change the value of a copy of the number?

I’m guessing that for some reason, the array is referred to by reference and the number by value, but why? How can I know what behavior to expect with other objects?

Subpart array affects main array by individual replacement, but does not affect main array when vectorized

Why does the subpart array affects main array by individual replacement, but does not affect main array when vectorized? For example if the code is: import numpy as np Z =np.array( [[0,4,0,0,0,0], [

why does array slice convert javascript arguments to array

Why does applying the slice method to the javascript arguments value as follows Array.prototype.slice.call(arguments) convert it to an array? If slice is used on arrays, and arguments is not an array,

JavaScript: why does changing an argument variable change the `arguments` “array”?

Consider: > function hello(what) { . what = world; . return Hello, + arguments[0] + !; . } > hello(shazow) Hello, world! Why does changing the value of what change the value of argum

Why does “,,,” == new Array(4) [duplicate]

Possible Duplicate: Why does “,,,” == Array(4) in Javascript? In JavaScript why does ,,, == new Array(4) It returns true in Chrome Developer Tools, and nodejs console.

Why does “,,,” == Array(4) in Javascript?

Boot up your interpreter/console and try the comparison > ,,, == Array(4) True Why? At first I thought maybe since you could think of ,,, as an array of four characters with a ‘/0’ terminating

Array creation, why does this work?

I understand why : output = new Array(); and output = []; but why does this work? output = Array();

Why does javascript turn array indexes into strings when iterating?

This Javascript logic puzzles me. I’m creating an array and setting the first element of it to a number. When I interate through it using a for loop Javascript turns the array key into a string. Why

Does $.grep affect the original array? [closed]

I’m filtering an array which forms a set of markers on a Google Map (API V3) here: http://testdae.dialanexchange.com/testmap.aspx. It all works in combination correctly for either of Timeshare Resorts

Why does changing one array alters the other?

Consider this tiny bit of javascript code: var a = [1, 2, 3], b = a; b[1] = 3; a; // a === [1, 3, 3] wtf!? Why does a change when I update b[1]? I’ve tested it in Firefox and Chrome. This doesn’t

Javascript array length not changing

I’ve got an array of an object, that doesn’t seem to update in length. What am I not knowing about Javascript arrays? http://stackoverflow.pastebin.com/aqZtRkkf The length is reported as 0, and it sho


Everything is copied by reference except primitive data types (strings and numbers IIRC).

Well, the only possible answer — and the correct one — is that you’re not actually copying the array. When you write

var copyOfArray = array;

you’re assigning a reference to the same array into another variable. They’re both pointing at the same object, in other words.

You don’t have any copies.
You have multiple variables holding the same array.

Similarly, you have multiple variables holding the same number.

When you write copyOfMyNumber = …, you’re putting a new number into the variable.
That’s like writing copyOfMyArray = ….

When you write copyOfMyArray.splice, you’re modifying the original array.
That isn’t possible with numbers because numbers are immutable and cannot be modified,

An array in JavaScript is also an object and objects are always passed/assigned by reference. Thus both variables have a reference to the same object.

Your comparison with the number example is not correct btw. You assign a new value to copyOfMyNumber. If you assign a new value to copyOfMyArray it will not change myArray either.

You can create a copy of an array using slice [docs]:

var copyOfMyArray = myArray.slice(0);

But note that this only returns a shallow copy, i.e. objects inside the array will not be cloned.

In JS, operator “=” copy the pointer to the memory area of the array. If you want to copy an array into another you have to use the Clone function.

For integers is different because they are a primitive type.


In javascript, an array is typed as an object, whereas a number is typed as a number – a primitive data type. When you set a variable equal to an array, you are only passing reference to it. If you truly need to copy it – you could do something like

for (var i = 0, i < originalArray.length, i++){

check out the snooks for more on this topic.