Copy one array to another in javascript

These code snippets help you to copy elements of one array into another array in javascript.

Answers 2
  • Copying one arrray to another in javascript using spread syntax

    <script>  
    let array1 = ['s', 'u', 'm', 'm', 'e', 'r'];
    let array2 = [...array1];
    console.log(array2);
    </script>
    
    0

    Output:

    [ 's', 'u', 'm', 'm', 'e', 'r' ]

    You can copy on array elements to another array using (...) spread syntax in javascript.

  • Copying one array into another in javascript using spread syntax

    <script> 
    let seasons = ['spring', 'summer'];
    let seasons2 = ['autumn', ...seasons, 'winter'];
    console.log(seasons2); 
    </script>
    
    0

    Output:

    [ 'autumn', 'spring', 'summer', 'winter' ]

    This code snippet will help you to add the array elements at the specific place to the existing array (array with existing elements).

  • Back to code snippet queries related javascript