JavaScript .copyWithin()
The .copyWithin() method returns a shallow copy of an array where one subarray replaces another part without modifying the original length. However, since it is a mutating method, .copyWithin() will change the array’s contents and create or delete properties if necessary.
Syntax
myArray.copyWithin(target);
myArray.copyWithin(target, start);
myArray.copyWithin(target, start, end);
The following parameters can be applied to the .copyWithin() method:
target(required): A zero-based index to copy the sequence to.- If
targetis negative, it counts back frommyArray.lengthandtarget + myArray.lengthis used. - If
targetis <myArray.length * -1,0is used. - If
targetis >=myArray.length, nothing is copied. - If, after
myArrayis normalized, thetargetis positioned afterstart, copying only occurs until the end ofmyArray.length(i.e.,.copyWithin()never extends themyArray).
- If
start(optional): A zero-based index at which to start copying the elements from.- If
startis negative, it counts back from the end ofmyArrayandstart + myArray.lengthis used. - If
start<myArray.length * -1or it is omitted,0is used. - If
start>=myArray.length, nothing is copied.
- If
end(optional): A zero-based index at which to end copying elements from (non-inclusive).- If
endis negative, it counts back from the end ofmyArrayandend + myArray.lengthis used. - If
end<myArray.length * -1,0is used. - If
end>=myArray.lengthor it is omitted,myArray.lengthis used and all elements until the end are copied. - If
endis positioned before or atstartafter normalization, nothing is copied.
- If
Sparse Arrays
If the .copyWithin() method is applied to a sparse array that contains empty slots, it will populate them with an undefined value:
console.log([1, , 3].copyWithin(2, 1, 2));// Output: [1, undefined, undefined]
Example
The following examples has a few calls to the .copyWithin() method, applying combinations of the available parameters:
// copy elements starting from the second-to-last indexconsole.log([1, 2, 3, 4, 5].copyWithin(-2));// copy to index 0 all elements index 3 to the endconsole.log([1, 2, 3, 4, 5].copyWithin(0, 3));// copy to index 0 the element at index 2console.log([1, 2, 3, 4, 5].copyWithin(0, 2, 3));// copy to second-to-last index elements starting from index 2 until end of arrayconsole.log([1, 2, 3, 4, 5].copyWithin(-2, -3, -1));
This will produce the following output:
[ 1, 2, 3, 1, 2 ][ 4, 5, 3, 4, 5 ][ 3, 2, 3, 4, 5 ][ 1, 2, 3, 3, 4 ]
Codebyte Example
The following example uses the .copyWithin() method to copy the second element at index 1 (‘b’) to the fourth element at index 3:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript on Codecademy
- Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
- Includes 34 Courses
- With Professional Certification
- Beginner Friendly.115 hours
- Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
- Beginner Friendly.15 hours