QP School

Full Version: JavaScript Array copyWithin() example
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Code:
const fruits1 =
      [
        "Banana",
        "Orange",
        "Apple",
        "Mango"
      ];

const fruits2 =
      [
        "Banana",
        "Orange",
        "Apple",
        "Mango"
      ];

// Copy the first two elements to the last two elements

let text = fruits1.copyWithin(2, 0);

document.getElementById("output1").innerHTML = text;

// Copy the last two elements to the first two elements

let text2 = fruits2.copyWithin(0, 2);

document.getElementById("output2").innerHTML = text2;