I created a constant named 'alphabet' and assigned it to an array containing the first 5 letters of the alphabet. I then wanted to have a function that would add a number to each character in the array, to enumerate them. But, I didn't want the values in the original 'alphabet' array to be modified, so I created a 'temp' variable within my enumerating function and only made changes to that. However, any changes I made to 'temp' spread over to 'alphabet'. I don't understand why and I'd like to prevent that from happening.
Here's my code: (also available on CodePen)
const alphabet = ["a", "b", "c", "d", "e"];
function alphaPosition(seq) {
//'temp' gets 'seq' to avoid making changes directly on the provided argument.
let temp = seq;
//adds indexes to each element in the 'temp' array:
for (let i = 1; i <= temp.length; i++) {
temp[i - 1] = temp[i - 1] + i;
}
return temp;
}
console.log(
"Step 1. 'alphabet' array before running the 'alphaPosition' function:"
);
console.log(alphabet);
console.log(
"Step 2. This is the final value of 'temp' in 'alphaPosition' after running the function. An index has been added to every element in the array, as expected:"
);
console.log(alphaPosition(alphabet));
console.log(
"Step 3. Here's the 'alphabet' array after running 'alphaPosition'. Indexes have also been added to every element, despite not modifying the function argument directly:"
);
console.log(alphabet);
Output:
/*
-> Step 1. 'alphabet' array before running the 'alphaPosition' function:
-> ["a", "b", "c", "d", "e"]
-> Step 2. This is the final value of 'temp' in 'alphaPosition' after running the function. An index has been added to every element in the array, as expected:
-> ["a1", "b2", "c3", "d4", "e5"]
-> Step 3. Here's the 'alphabet' array after running 'alphaPosition'. Indexes have also been added to every element, despite not modifying the function argument directly:
-> ["a1", "b2", "c3", "d4", "e5"]
*/
Why do changes to 'temp' spread to 'alphabet'? I would expect that, since I defined 'alphabet' as a constant, it shouldn't even be possible to modify it. Also, I never make changes to it in my function. I only use it to define 'temp'. Is there any way to prevent these spreads from happening?
I experimented doing something similar with a numeric const instead of an array, and everything worked as intended:
const number = 10;
function numChange(n) {
//'virtualN' gets 'n' to avoid making changes directly on the provided argument.
let virtualN = n;
//modify 'virtualN' multiple times to emulate what was done to the 'temp' array in the alphaPosition function.
for (let i = 1; i <= 5; i++) {
virtualN = "iteration" + i;
}
return virtualN;
}
console.log(
"Step 1. See the value of 'number' before running the numChange function:"
);
console.log(number);
console.log(
"Step 2. This is the final value of 'virtualN' in 'numChange(number)' after running the function. As expected, it's been modified from its initual value by the 'for' loop:"
);
console.log(numChange(number));
console.log(
"Step 3. Finally, we can see the value of 'number' is still the same as before running the numChange function. As expected, only the value of virtualN changed while the argument 'n' suffered no modifications:"
);
console.log(number);
Output:
/*
-> Step 1. See the value of 'number' before running the numChange function:
-> 10
-> Step 2. This is the final value of 'virtualN' in 'numChange(number)' after running the function. As expected, it's been modified from its initual value by the 'for' loop:
-> iteration5
-> Step 3. Finally, we can see the value of 'number' is still the same as before running the numChange function. As expected, only the value of virtualN changed while the argument 'n' suffered no modifications:
-> 10
*/
Why does using an intermediary variable to avoid making changes to the original work for numbers and not for arrays?
I'd be extremely grateful for any help or clarification on this. I've added my code to this CodePen in case you'd like to tweak it or experiment more with it. Thank you in advance for any help.