What is copywithin in javascript?

 Array copywithin method javascript:

 

copyWithin() method returns a new array and changes the original array. It does not change original indexing.

Syntax of copyWithin method:

array.copyWithin(target, start, end)

 

copywithin example in javascript:

 

const colors = ["Red", "blue", "Black", "Orange", "White", "Coffee", "Green"];

 

console.log(colors.copyWithin(-2));

// replace from the -2 position means from the last

// Array ["Red", "blue", "Black", "Orange", "White", "Red", "blue"]

 

console.log(colors.copyWithin(0, 2, 5));

// copy to index 0 the element at index 2

// Array ["Black", "Orange", "White", "Orange", "White", "Coffee", "Green"]

 

console.log(colors.copyWithin(1, 2));

// copy to index 1 all elements from index 2 to the end

// Array ["Black", "White", "Orange", "White", "Coffee", "Green", "Green"]

 

console.log(colors);

//Array ["Black", "White", "Orange", "White", "Coffee", "Green", "Green"]




No comments:

Note: Only a member of this blog may post a comment.

Copyright Reserved to Anything Learn. Powered by Blogger.