Skip to content

Instantly share code, notes, and snippets.

@weiland
Created April 7, 2015 10:31
Show Gist options
  • Save weiland/0056b44c75dd6158db75 to your computer and use it in GitHub Desktop.
Save weiland/0056b44c75dd6158db75 to your computer and use it in GitHub Desktop.
Clone an Array in JavaScript
// extending the Array prototype
Array.prototype.clone = function clone() {
return this.slice(0);
};
// example
var arr = [4,5];
var copy = arr.slice(0);
// better (since we do not extend the prototype):
function cloneArray(arr) {
return arr.slice(0);
}
var arr = [4,5];
var copy = cloneArray(arr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment