some helpful links:
these examples side-by-side on JS Tutor
Mutating Array Methods
(modify the original array)
print the array:
mutated;
(read the docs)
add to the end:
mutated.push(newValue);
(read the docs)
remove from the end:
mutated.pop();
(read the docs)
add to the beginning:
mutated.unshift(newValue);
(read the docs)
remove from the beginning:
mutated.shift();
(read the docs)
reverse the array:
mutated.reverse();
(read the docs)
sort the array:
mutated.sort();
(read the docs)
remove entries:
mutated.splice(a,b,c);
(read the docs)
Mutating Array History
Non-Mutating Array Methods
(create a new, modified array)
print the original array:
nonMutated;
(read the docs: Array)
add to the end:
const pushedCopy = [...nonMutated, newValue];
remove from the end:
const poppedCopy = nonMutated.slice(0, -1);
(read the docs: .slice())
add to the beginning:
const unshiftedCopy = [newValue, ...nonMutated];
remove from the beginning:
const shiftedCopy = nonMutated.slice(1);
reverse the array:
const reversedCopy = [...nonMutated].reverse();
(read the docs: .reverse())
sort the arrray:
const sortedCopy = [...nonMutated].sort();
(read the docs: .sort())
remove entries:
const splicedCopy = [...nonMutated.slice(0, a), c, ...nonMutated.slice(a + b)];
Non-Mutating Array History