JavaScript: Pass By Reference and By Value??

steveleung9527
2 min readMay 16, 2022

--

Passing by value and passing by reference are little bit confusing in JavaScript, especially if you are new to programming.

What is Pass by reference?

In computer data stored in the variable you created are actually stored in computer ram with particular address. When the variable is passed into a function as parameter, it is actually pass its address in ram. So if you mutate the variable value, it will not create a new variable but change the original one’s value.

What is Pass by value?

Not like Pass by reference, Pass by value means that when a variable is passed in a function as parameter is actually pass its own value into it. Whatever it changed in the function, the original one would not be changed. Let’s see some example in JavaScript for better understanding

Pass by Reference

Now you have some basic understanding of pass by reference and value. And you should also realize that why do i use different data type (array vs number) as example. Here is why.

In JavaScript, “Object” are enforced to pass by reference, and “Primitives” are enforced to pass by value. And you can never change it.

So how can we overcome this problem?? Here will provide some solution.

solution

Class Instance In JS

Class In JavaScript

solution

Class Instance Copy

Follow me if you find the information is useful, thanks.

--

--