What is the 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: 23-May-2020

116 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements