This JavaScript tutorial explains how to use the Array technique known as copyWithin() with syntax and examples.
Description
In JavaScript, copyWithin() is an Array method that is used to replica a element of an array from one area in the array to any other vicinity in the equal array. Because the copyWithin() approach is a technique of the Array object, it ought to be invoked via a unique instance of the Array class.
Syntax
In JavaScript, the syntax for the copyWithin() technique is:
array.copyWithin(destination, copy_start, copy_end);
Parameters or Arguments
destination The index vicinity in the array to copy a component of the array to. If destination is negative, the vacation spot will be decided by way of applying the index place (in reverse) starting from the stop of the array. copy_start Optional. The index position the place the reproduction of the elements will begin. If copy_start is negative, the index role will be applied (in reverse) starting from the end of the array. If this parameter is no longer provided, it will default to 0. copy_end Optional. The index position the place the reproduction of the factors will end, but does no longer including the ending component itself. If copy_end is negative, the index function will be applied (in reverse) beginning from the give up of the array. If this parameter is no longer provided, it will default to arr.length.
Returns
The copyWithin() technique returns the modified array with the factors copied as distinct via the destination, copy_start and copy_end parameters.
Note
The copyWithin() method modifies the authentic array but it does now not change the length of the array.
Example
Let’s take a appear at an instance of how to use the copyWithin() technique in JavaScript.
For example:
var totn_array = ['totn','a','b','c','d','e'];
console.log(totn_array.copyWithin(5, 0, 1));
In this example, we have declared an array object known as totn_array that has 6 elements. We have then invoked the copyWithin() method of the totn_array variable to alter this array.
We have written the output of the copyWithin() method to the internet browser console log, for demonstration purposes, to show what the copyWithin() approach returns.
The following will be output to the internet browser console log:
["totn", "a", "b", "c", "d", "totn"]
In this example, the copyWithin() method will return the modified array after copying the first issue “totn” at index role zero into the closing aspect at index position 5.
Using Negative Parameter Values
When you use negative parameters, the copyWithin() technique will decide the index positions starting from the cease of the array.
For example:
var totn_array = ['a','b','c','d','e','totn'];
console.log(totn_array.copyWithin(-2, -1));
The following will be output to the web browser console log:
["a", "b", "c", "d", "totn", "totn"]
In this example, the copyWithin() approach will return the modified array after copying the final factor “totn” at index function -1 into the 2d ultimate thing at index position -2.
Leave a Review