Skip to content

Instantly share code, notes, and snippets.

@kaplanmaxe
Created February 19, 2017 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaplanmaxe/c9c27234d8b1ddac288838c6290af29e to your computer and use it in GitHub Desktop.
Save kaplanmaxe/c9c27234d8b1ddac288838c6290af29e to your computer and use it in GitHub Desktop.
JS Arrays are By Reference by default
const a = [0, 1, 2, 3];
const b = a;
b.push(10);
console.log(b); // [0, 1, 2, 3, 10]
console.log(a); // [0, 1, 2, 3, 10] (10 got pushed to a even though we only pushed to b)
// To AVOID passing by reference
const foo = [0, 1, 2, 3];
const bar = foo.concat([10]);
console.log(bar); // [0, 1, 2, 3, 10]
console.log(foo); // [0, 1, 2, 3] (10 did not get pushed here)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment