Created
February 19, 2017 13:20
-
-
Save kaplanmaxe/c9c27234d8b1ddac288838c6290af29e to your computer and use it in GitHub Desktop.
JS Arrays are By Reference by default
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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