How to copy an array JavaScript

In JavaScript variables are immutable, but arrays are mutable. In other words, if you copy an array(array2 = array1;), the new array will retain changes made to the old array. To clone an array that won’t be linked to the original you need to make a shallow copy:

array2 = array1.slice(0);

Advertisements

Published by

angelo bonavera

Hi I'm a web developer and I run a web hosting company called 2slick.com.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s