The copyWithin function replicates a portion of an array within the same array and establishes a new starting position at the specified target. This method is mutable, meaning it modifies the array in place. While it does not change the overall length of the array, it can modify its content and may introduce new properties if required. The copyWithin method accepts three parameters: two that are compulsory and one that is optional.
Syntax:
arr.copyWithin(target)
arr.copyWithin(target, start)
arr.copyWithin(target,start,end)
Parameters:
Target: The specific index location where the elements will be copied. (Mandatory).
Initiate the process of copying elements from the specified index position. (Optional)
End: This parameter is not mandatory. It specifies the index position in the source from which to cease copying elements.
Return value:
The modified array.
Browser Support:
| Chrome | 45.0 |
|---|---|
Edge |
12.0 |
| Firefox | 32.0 |
| Opera | NO |
Example 1
JavaScript TypedArray copyWithin(target) Method
<script type="text/javascript">
// Input array
// JavaScript to illustrate copyWithin() method
var arr1= [1,2,3,4,5,6,7,8,9,10];
arr1.copyWithin(2)
//Placing from index position 2
//The element from index 0
document.write(arr1);
// expected output: arr1 [Output:1,2,1,2,3,4,5,6,7,8]
</script>
Output:
1,2,1,2,3,4,5,6,7,8
Example 2
The copyWithin(target, start) method in JavaScript's TypedArray allows you to copy a sequence of elements within the same TypedArray, effectively overwriting a portion of it. This method is particularly useful when you want to manipulate data in a more efficient way without the need to create a new array.
Syntax
typedArray.copyWithin(target, start);
Parameters
- target: This is the index in the TypedArray at which the copying process will begin. It can be a positive or negative integer.
- start: This parameter denotes the index from which the elements will be copied. Like the target parameter, it can also be a positive or negative integer.
Description
The copyWithin method enables you to copy elements from a specified start index to a target index. If the start index is negative, it counts backward from the end of the TypedArray. The copying process will overwrite the elements starting from the target index.
Return Value
The method returns the modified TypedArray after the copying is complete.
Examples
Here are a few illustrative examples regarding the usage of the copyWithin method:
- Basic Usage
- Using Negative Indices
- Specifying a Length for Copying
let typedArray = new Uint8Array([1, 2, 3, 4, 5]);
typedArray.copyWithin(0, 3); // Copies elements from index 3 to target index 0
console.log(typedArray); // Output: Uint8Array(5) [ 4, 5, 3, 4, 5 ]
let typedArray = new Uint8Array([1, 2, 3, 4, 5]);
typedArray.copyWithin(0, -2); // Copies elements from the second last index
console.log(typedArray); // Output: Uint8Array(5) [ 4, 5, 3, 4, 5 ]
let typedArray = new Uint8Array([1, 2, 3, 4, 5]);
typedArray.copyWithin(0, 3, 4); // Copies elements from index 3 to index 0, but only length 1
console.log(typedArray); // Output: Uint8Array(5) [ 4, 2,
Output:
1,2,4,5,6,7,8,9,10,10
## Example 3
The copyWithin(target, start, end) method of JavaScript's TypedArray allows you to copy a sequence of elements within the array itself. This method modifies the existing array by copying a portion of its values from one location to another, effectively overwriting values in the process.
### Syntax
typedArray.copyWithin(target, start, end);
### Parameters
- **target**: This parameter indicates the index at which the copied elements will be inserted. If the specified index is negative, it is treated as an offset from the end of the array.
- **start**: This parameter specifies the index from which to begin copying elements. As with the target, if a negative index is provided, it counts backward from the end of the array.
- **end** (optional): This parameter defines the index at which to stop copying elements. The copy operation will include elements up to, but not including, this index. If omitted, the method will copy all elements to the end of the TypedArray. Negative indices can also be utilized here.
### Return Value
The method returns the modified TypedArray instance.
### Description
The copyWithin method performs a shallow copy of part of the TypedArray to another location within the same array. The original elements at the target location will be overwritten by the copied elements.
### Example
Here’s an example demonstrating the use of the copyWithin method:
const typedArray = new Uint8Array([1, 2, 3, 4, 5]);
typedArray.copyWithin(0, 3); // Modifies the array to [4, 5, 3, 4, 5]
console.log(typedArray); // Output: Uint8Array(5) [4, 5, 3, 4, 5]
### Additional Example with start and end
You can also specify both start and end parameters to control the range of elements to be copied:
const typedArray2 = new Uint8Array([1, 2, 3, 4, 5]);
typedArray2.copyWithin(0, 1, 4); // Modifies the array to [2, 3, 4, 5, 5]
console.log(typedArray2); // Output: Uint8Array(5) [2, 3, 4, 5, 5]
### Notes
-
Output:
1,3,4,4,5,6,7,8,9,10