C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
JavaScript TypedArray copyWithin() MethodThe copyWithin() method copies the sequence of array within the array and set a new starting point at target. The copyWithin() method is a mutable method and update the array directly. It does not alter the length of array, but will change its content and create new properties if necessary. This method have three parameters, two mandatory and one optional. Syntax:arr.copyWithin(target) arr.copyWithin(target, start) arr.copyWithin(target,start,end) Parameters:Target: The index position to copy the elements to. (Required). Start: The index position elements are started copying from. (Optional) End: It is optional. Source end index position where to end copying elements from. Return value:The modified array. Browser Support:
Example 1JavaScript 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 2JavaScript TypedArray copyWithin(target,start) 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,3) //Placing from index position 2 // Element from index 3 document.write(arr1); // expected output: arr1 [Output: 1,2,4,5,6,7,8,9,10,10] </script> Output: 1,2,4,5,6,7,8,9,10,10 Example 3JavaScript TypedArray copyWithin(target,start,end ) 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(1,2,4) // Placing at index position 1 // Element between index 2 and 4 document.write(arr1); // expected output: arr1 [Output: 1,3,4,4,5,6,7,8,9,10] </script> Output: 1,3,4,4,5,6,7,8,9,10
Next TopicJavaScript TypedArray Object
|