JavaScript Array Shallow Copy

Example 1:
class js {
    constructor() {
        this.mainVal = [];
    }
    copy(val) {
       this.mainVal = val; 
    }
}
const a = [
  "India",
  "United States",
  "Germany"
];
js.prototype.copy(a);
console.log(js.prototype.mainVal);
a[1] = "France";
console.log(a);
console.log(js.prototype.mainVal);
Output:
[ 'India', 'United States', 'Germany' ]
[ 'India', 'France', 'Germany' ]
[ 'India', 'France', 'Germany' ]

Try it

Learn More