Copy by value for a Composite Data type in JavaScript

--

In general, Primitive Data Types(String, Boolean..) are passed By Value and Composite Data Types(objects) are passed By Reference.

But how to pass objects by value ??

Well, there are multiple ways for achieving pass by value for objects. one of them is using spread separator. The spread operator (...) is from modern JavaScript (ES6 and higher) that can be used to clone an array by "spreading" its elements.

Array :

In line 2, all the elements of array are assigned to new variable copyOfArrayByValue using spread operator(…)

Object :

All the properties of the object are assigned to new object using spread operator.

--

--