Usage of copyWithin Method in JavaScript



Use the copyWithin() method in JavaScript to copy array elements. You can set the index from/to where (index) the elements are copied. The following are the elements supported by JavaScript.

  • target − The position of the index from where the elements is to be copied.
  • begin − The index to start copying the element. This is an optional parameter.
  • end − The index to end copying the element. This is an optional parameter.

Example

You can try to run the following code to learn how to work with copyWithin() method in JavaScript.

<!DOCTYPE html>
<html>
   <body>
      <script>
         var num = ["One", "Two", "Three", "Four", "Five", "Six", "Seven"];
         document.write(num);

         document.write("<br>"+num.copyWithin(3,0,4));
      </script>
   </body>
</html>
Updated on: 2020-05-23T11:00:01+05:30

156 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements