How to copy by value a composite data type in JavaScript ?

Mohammed Saif
4 min readDec 3, 2020

Each programming language depends on utilizing variables to store and control information, hence declaration and handling of variables turns out to be significant in any programming language.

JavaScript is truly adaptable while declaring these variables as it allows loose typing and assigns the data type without help from anyone else, data types fundamentally determine what sort of information can be stored and controlled inside a program.

JavaScript supports three fundamental data types

1. Primitive data type : it permits you to store one value at a time, String ,Number and Boolean are the primitive data types

Primitive data type

2. Composite data type: this data type permits storing collection of values and more complex entities such are Objects, Array and Functions.

Composite data type

3. Special data types: besides normal data types JavaScript additionally has some unique types like Undefined, Null and NAN which are extremely helpful in error handling.

Declaring a undefined data type
Output from the execution

Copying of Variables : There are two ways by which the data can be copied which are

  1. Copy by Value: This type of copy is upheld in primitive types, where the information present in a variable is copied to another variable and stored in different memory areas, the two variables are independent of one another and change in both of their values doesn’t affect the other.
Example for copy by value
Execution output

2. Copy by Reference: this kind of copying is used by composite data type, where a reference of the first variable is stored in the copied variable, the original data is stored in one single memory area yet will have 2 variables pointing to the same memory address, here the variables are reliant on one another.

As should be obvious, the composite data type is supporting just copy by reference but in JavaScript we can copy the composite data type by utilizing strategies discussed beneath:

1. Using Spread Operator: it permits an iterable to expand in places where multiple arguments are expected, It gives us the to obtain a list of parameters from an array. This is also known as shallow copy in JavaScript and it is most applicable to composite data like arrays and objects.

In the example the change in the copied array does not impact the original array and thus behaving like copy by value.

2. Using Object.assign() method: this method copies all enumerable properties from one or more sources to the target object, it is a shallow copy. It creates a true copy.

3. Using JSON.parse() and JSON.stringify(): The JSON handler has two special methods called parse and stringify which can be used to transform a composite data type into primitive object, we can achieve copy by value using these methods in sync it behaves as a deep copy.

--

--

Mohammed Saif

I am a software developer and a passionate full stack developer, I enjoy coding and problem solving and here to share my learning experience and knowledge