Skip to content

Instantly share code, notes, and snippets.

@buglessir
Created March 23, 2019 10:46
Show Gist options
  • Select an option

  • Save buglessir/cfed9d06a0459fdefcf9d5eaccb8d5f0 to your computer and use it in GitHub Desktop.

Select an option

Save buglessir/cfed9d06a0459fdefcf9d5eaccb8d5f0 to your computer and use it in GitHub Desktop.
function immutable(originalArray) {
// Instead of mutating/modifying the original array,
// we first make a copy of the original array
// In this way, the original array stay unchanged.
var newArray = [...originalArray];
newArray[0] = "new value";
return newArray;
}
var array = ["some value", "another value"];
alert("Return from immutable " + immutable(array));
alert("Array: " + array + " (original array stay unchanged).");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment