How to copy by value a composite data type

Mounikadasari
2 min readNov 5, 2020

Firstly we need to know…….

  • What is a data type??

A data type is an attribute of data which tells the compiler how the programmer intends to use the data.

A data type constrains the values that an expression, such as a variable or a function, might take. data types are used within type systems, which offer various ways of defining, implementing, and using them.

JavaScript provides different data types to hold different types of values. There are two types of data types in JavaScript:

  1. Primitive data types(call by value)
  2. Composite data types(call by reference)
  3. trivial
  • Primitive data types:

Primitives are known as being immutable data types because there is no way to change a primitive value once it gets created.

The primitive data types in JavaScript are…

  1. Numbers
  2. Boolean
  3. strings
var p=500;
var q=p;
p=1000;
console.log(p)//1000
console.log(q)//500

here; in the above example the value is assigned for p and also copy value of p is also created and assigned it for “q”, when we change the value of “p”, the value of “q” doesn’t change because the value of “P” is copied before changing the value of “P”, so the value of “q” remains same and value of “P” changes.

  • Composite data types:

composite data types are mutable data types. The value of an object can be changed after it gets created.

The composite data types are….

  1. Objects
  2. Functions
  3. Arrays
var mydetails={"name":"mounika","age":"19","place":"guntur"}
var myguvidetails=mydetails;
myguvidetails.name="guvi_student_mounika";
console.log(mydetails);
//{name: "guvi_student_mounika", age: "19", place: "guntur"}
console.log(myguvidetails);
//{name: "guvi_student_mounika", age: "19", place: "guntur"}

“Composite data types are a combination of primitives and other datatypes” They include arrays, lists, and collections. Variables that are assigned a non-primitive value are given a reference to that value. That reference points to the object’s location in memory. The variables don’t actually contain the value.

  • Trivial data types:

There are special data types in JavaScript i.e; null and undefined

→Null type has only one special value null. Logically, null value is an empty object pointer, which returns “object” when passed.When defining a variable that is meant to later hold an object, it is advisable to initialize the variable to null as opposed to anything else.

→Undefined type has only one special value undefined. When a variable is declared using var but not initialized

var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
person = null;
console.log(person);//prints null
var a=500;
var b;
console.log(a);//prints 500
console.log(b);//prints undefined

How do you copy by value a composite data type???

As we know that, primitive data types are copy by value and composite data types are copy by reference .we can copy by value a composite data type by using spread operator[…]

var dogbreed = ["pumerian","pug","beagle"];
var mydogbreed =[...dogbreed]
mydogbreed[0]="shitzu";
console.log(dogbreed);
//prints ["pumerian", "pug", "beagle"]
console.log(mydogbreed);
//prints ["shitzu", "pug", "beagle"]

--

--