JavaScript Clone an Array example

If you want to clone an Array in Javascript we can use the spread operator to expand our array into items and clone it into…

The post JavaScript Clone an Array example appeared first on CodeSource.io.


This content originally appeared on CodeSource.io and was authored by Deven

If you want to clone an Array in Javascript we can use the spread operator to expand our array into items and clone it into a new array:

consider the following example:

const numbers = [8, 22, 52, 100, 6, 15];
const numbersCopy = [...numbers];

Another better approach is using  Array.slice() method with no arguments. consider the following example.

const numbers = [8, 22, 52, 100, 6, 15];
const numbersCopy = numbers.slice();

The post JavaScript Clone an Array example appeared first on CodeSource.io.


This content originally appeared on CodeSource.io and was authored by Deven


Print Share Comment Cite Upload Translate Updates
APA

Deven | Sciencx (2021-02-08T14:20:01+00:00) JavaScript Clone an Array example. Retrieved from https://www.scien.cx/2021/02/08/javascript-clone-an-array-example/

MLA
" » JavaScript Clone an Array example." Deven | Sciencx - Monday February 8, 2021, https://www.scien.cx/2021/02/08/javascript-clone-an-array-example/
HARVARD
Deven | Sciencx Monday February 8, 2021 » JavaScript Clone an Array example., viewed ,<https://www.scien.cx/2021/02/08/javascript-clone-an-array-example/>
VANCOUVER
Deven | Sciencx - » JavaScript Clone an Array example. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/02/08/javascript-clone-an-array-example/
CHICAGO
" » JavaScript Clone an Array example." Deven | Sciencx - Accessed . https://www.scien.cx/2021/02/08/javascript-clone-an-array-example/
IEEE
" » JavaScript Clone an Array example." Deven | Sciencx [Online]. Available: https://www.scien.cx/2021/02/08/javascript-clone-an-array-example/. [Accessed: ]
rf:citation
» JavaScript Clone an Array example | Deven | Sciencx | https://www.scien.cx/2021/02/08/javascript-clone-an-array-example/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.