JavaScript Array copyWithin() method
The 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.
Syntax
The copyWithin() method is represented by the following syntax:
Parameter
target – 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.
Return
The modified array.
JavaScript Array copyWithin() method example
Let’s see some examples of copyWithin() method.
Example 1
Here, we will pass the target, start and end index with the method.
Output:
Node.js,Node.js,JQuery,Bootstrap
Example 2
Let’s see one more example where we will copy two elements.
Output:
Node.js,JQuery,JQuery,Bootstrap
Example 3
In this example, we will provide only the target index and start index.
Output:
AngularJS,JQuery,Bootstrap,Bootstrap
Example 4
In this example, we will provide target index only.
Output:
AngularJS,Node.js,AngularJS,Node.js