Skip to content

Instantly share code, notes, and snippets.

@thejsj
Created December 12, 2016 07:02
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 thejsj/0b19934017c7b61fe74ac89b2847cc33 to your computer and use it in GitHub Desktop.
Save thejsj/0b19934017c7b61fe74ac89b2847cc33 to your computer and use it in GitHub Desktop.
Javascript Cloning for Arrays
var a = [1, 2, 3]
var b = a // b now points to the same array as a
a === b // true
var c = [...a] // Makes a shallow copy of the array
c === a // false
a.push(4)
b // [1, 2, 3, 4]
c // [1, 2, 3]
a = null
b // [1, 2, 3, 4] Still point to the same array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment