Difference between copy by value and copy by reference.

Ravi Teja
Dec 15, 2020

--

1 .Copy by value:

  1. All primitive datatypes are copied by their value
  2. they are:

example:1

let x=10;

let y=x;

console.log(x);//it prints 10

console.log(y);//it prints 10

example:2

let x=10;

let y=x;

x=20;

console.log(x);//it prints 20 console.log(y);//it prints 10

3.they are INDEPENDENT of each other

2.Copy by reference:

  1. objects are coppied by their Reference
  2. they are:

example:

let x={num:10};

let y=x;

x.num=50; //or y.num=50;

console.log(x.num);

//it prints 50

console.log(y.num);

//it prints 50

3. when we modify the object in (x OR y) .that changes are immediately reflect to another variable also.

example:

--

--