JavaScript | Array copyWithin()
The copyWithin() method copies part of an array to the same array and returns it, without modifying its size i.e, copies array element of a array within the same array.
Syntax:
array.copyWithin(target, start, end) Parameters: target : The index position to copy the elements to(Required). start : It is optional. The index position to start copying elements from (default is 0). end : It is optional. The index position to stop copying elements from (default is array.length). Return value: It returns the modified array.
JavaScript Version : ECMAScript 6
Browser Support:
Chrome 45.0 Edge 12.0 Firefox 32.0 Opera 32.0 IE No
Examples
Program 1:
// Input array var array = [ 1, 2, 3, 4, 5, 6, 7 ]; // placing at index position 0 the element // between index 3 and 6 console.log(array.copyWithin(0, 3, 6)); |
Output:
Array [4, 5, 6, 4, 5, 6, 7]
Program 2:
// Input array var array = [ 1, 2, 3, 4, 5, 6, 7 ]; // placing from index position 0 the // element from index 4 console.log(array.copyWithin(0, 4)); |
Output:
Array [5, 6, 7, 4, 5, 6, 7]
Program 3:
// Input array var array = [ 1, 2, 3, 4, 5, 6, 7 ]; // placing from index position 3 // the element from index 0 console.log(array.copyWithin(3)); |
Output:
Array [1, 2, 3, 1, 2, 3, 4]
Application: Whenever we need to copy the content of any array to the same array that time we use array.copyWithin() element in javaScript.
// Input array var array = [ 1, 2, 3, 4, 5, 6, 7 ]; // placing at index position 0 the // element between index 4 and 5 console.log(array.copyWithin(0, 4, 5)); |
Output:
Array [5, 2, 3, 4, 5, 6, 7]
Recommended Posts:
- JavaScript | typedArray.copyWithin() with Examples
- RMS Value Of Array in JavaScript
- JavaScript | Array pop()
- How to convert Set to Array in JavaScript?
- JavaScript | Array every() function
- JavaScript | Array indexOf()
- How to use loop through an array in JavaScript?
- How to add an object to an array in JavaScript ?
- Get the first and last item in an array using JavaScript
- JavaScript | array.keys()
- How to Convert Array to Set in JavaScript?
- JavaScript | Array filter()
- JavaScript | Array some() function
- JavaScript | Array from() Method
- JavaScript | array.reduceRight()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



