COPY(PASS) BY VALUE V/S COPY(PASS) BY REFERNCE IN JAVASCRIPT

Kamlesh Jambani
2 min readMar 22, 2021

Javascript basically has two types of data types one are primitive (Boolean,NULL,Undefined,Number)and other are (Object)non primitive(array,function ,object).

The primitive data types are passed(copied) by value because the variable contains the primitive value.

This how the execution of the code looks in the js engine,As you can see here variable x contains the value of 10 and variable y contains the value of ‘abc’.

so when we use ‘=’ to assign these variables to other variables,we copy the primitive value into the new variable.

As you can see the in the above example now both new variables a and b now contain the value 10 and ‘abc’ respectively.

on the other hand variables that are assigned a non-primitive value are given a address to that value. That address points to the object’s location in memory. The variables don’t actually contain the value.

therefore (Object) non primitive are passed(copied) by refernce, because it contains the address of the value.

This how the execution of the code looks in the js engine,As you can see here variable x contains the address of 10 and variable y contains the address of ‘abc’.

so when we use ‘=’ to assign these variables to other variables,we copy the addresses into the new variable.

As you can see the in the above example now both new variables a and b now contain the addresses 10 and ‘abc’ respectively.

If you find this useful please hit the like button.

--

--