Javascript Array copyWithin()

Introduction

The copyWithin() method shallow copies sub array to another location in the same array.

It returns the changed array without modifying its length.

The copyWithin() method is a mutable method.

It does not alter the length of this, but it will change its content.

arr.copyWithin(target[, start[, end]])
Parameter
Optional
Meaning
target



Not



Zero-based index to start copying.
If negative, target will be counted from the end.
If target is at or greater than arr.length, nothing will be copied.
If target is positioned after start, the copied sequence will be trimmed to fit arr.length.
start


Optional


Zero-based index to start copying.
If negative, start will be counted from the end.
If start is omitted, copyWithin will copy from index 0.
end



Optional



Zero-based index to end copying.
copyWithin copies up to but not including end.
If negative, end will be counted from the end.
If end is omitted, copyWithin will copy until the last index (arr.length).

copy the first two array elements to the last two array elements.

var languages = ["CSS", "HTML", "Java", "Javascript"];
console.log(languages);/*from w w  w .ja  va 2 s  .c o m*/
console.log(languages.copyWithin(2,0));

Copy the first two array elements to the third and fourth position:

var languages = ["CSS", "HTML", "Java", "Javascript", "Kiwi", "Papaya"];
console.log(languages);/*from w w  w .  j  a  va2  s .  co  m*/

console.log(languages.copyWithin(2,0,2));
let a = [1, 2, 3, 4, 5].copyWithin(-2);
console.log(a);// [1, 2, 3, 1, 2]

a = [1, 2, 3, 4, 5].copyWithin(0, 3)//from   w ww  .  j av  a  2s.c  o m
console.log(a);// [4, 5, 3, 4, 5]

a = [1, 2, 3, 4, 5].copyWithin(0, 3, 4)
console.log(a);// [4, 2, 3, 4, 5]

a = [1, 2, 3, 4, 5].copyWithin(-2, -3, -1)
console.log(a);// [1, 2, 3, 3, 4]

a = [].copyWithin.call({length: 5, 3: 1}, 0, 3)
console.log(a);// {0: 1, 3: 1, length: 5}

More examples

let ints = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
ints.copyWithin(5);//from ww w  .j  a v a 2  s .  c  o m
console.log(ints); // [0, 1, 2, 3, 4, 0, 1, 2, 3, 4] 

ints = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
ints.copyWithin(0, 5);
console.log(ints); // [5, 6, 7, 8, 9, 5, 6, 7, 8, 9] 

ints = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
ints.copyWithin(4, 0, 3);
console.log(ints); // [0, 1, 2, 3, 0, 1, 2, 7, 8, 9] 

ints = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
ints.copyWithin(2, 0, 6);
console.log(ints); // [0, 1, 0, 1, 2, 3, 4, 5, 8, 9]   

ints = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
ints.copyWithin(-4, -7, -3);
console.log(ints); // [0, 1, 2, 3, 4, 5, 3, 4, 5, 6]  

ints = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
ints.copyWithin(1, -15, -12);
console.log(ints); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; 


ints = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
ints.copyWithin(1, 12, 15);
console.log(ints); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; 


ints = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
ints.copyWithin(2, 4, 2);
console.log(ints); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; 

ints = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
ints.copyWithin(4, 7, 10)
console.log(ints); // [0, 1, 2, 3, 7, 8, 9, 7, 8, 9]; 



PreviousNext

Related