C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
JavaScript Array copyWithin() methodThe JavaScript array copyWithin() method copies the part of the given array with its own elements and returns the modified array. This method doesn't change the length of the modified array. SyntaxThe copyWithin() method is represented by the following syntax: array.copyWithin(target, start, end) Parametertarget - The position where the copied element takes place. start - It is optional. It represents the index from where the method starts copying elements. By default, it is 0. end - It is optional. It represents the index at which elements stops copying. By default, it is array.length-1. ReturnThe modified array. JavaScript Array copyWithin() method exampleLet's see some examples of copyWithin() method. Example 1Here, we will pass the target, start and end index with the method. <script> var arr=["AngularJS","Node.js","JQuery","Bootstrap"] // place at 0th position, the element between 1st and 2nd position. var result=arr.copyWithin(0,1,2); document.writeln(result); </script> Output: Node.js,Node.js,JQuery,Bootstrap Example 2Let's see one more example where we will copy two elements. </script> var arr=["AngularJS","Node.js","JQuery","Bootstrap"] // place from 0th position, the elements between 1st and 3rd position. var result=arr.copyWithin(0,1,3); document.writeln(result); </script> Output: Node.js,JQuery,JQuery,Bootstrap Example 3In this example, we will provide only the target index and start index. <script> var arr=["AngularJS","Node.js","JQuery","Bootstrap"]; // place from 1st position, the elements after 2nd position. var result=arr.copyWithin(1,2); document.writeln(result); </script> Output: AngularJS,JQuery,Bootstrap,Bootstrap Example 4In this example, we will provide target index only. <script> var arr=["AngularJS","Node.js","JQuery","Bootstrap"]; // place from 2nd position, the elements after 0th position. var result=arr.copyWithin(2); document.writeln(result); </script> Output: AngularJS,Node.js,AngularJS,Node.js
Next TopicJavaScript Array
|