Call by Value and Call by Reference in JavaScript


This is the Advanced JavaScript article series. In this article series, we are learning various concepts of JavaScript. You can read them here.
As the name suggests, in this article we will learn the concept of "Call by Value and Call by Reference". Though this is not at all a new concept and if you are familiar with the C family of languages then most probably you have learned this concept in the beginning of your programming days. Correct..? Let's learn this concept in JavaScript.
Call by Value in JavaScript
The concept of Call by Value is, when we want to pass any parameter as a function argument then there are two types of arguments. An argument that is passed when the function is called an Actual Argument and the argument that takes the value in the caller's function is called a Formal Argument.  Here is a sample example.
  1. //Here name is a formal argument  
  2. Function testFunction(name ){  
  3. }  
  4. //the string(Sourav) is an actual parameter.  
  5. testFunction('Sourav');  
  6. Now, we will implement the same concept in a real application.  
  7. <!DOCTYPE html>  
  8. <html xmlns="">  
  9. <head>  
  10. </head>  
  11. <body>  
  12.     <script>   
  13.         function testFun(name) {  
  14.             name = name + 'Kayal';  
  15.             console.log("Modified name:- "  + name);  
  16.         }  
  17.         var name = 'Sourav'  
  18.         testFun(name);  
  19.         console.log("Old Name:- " + name);         
  20.     </script>  
  21. </body>  
  22. </html>  
Call by value in JavaScript
Within the function we are modifying the Formal Argument value and then outside of the function we are printing the value of the old variable and we are seeing that the value within the function has been changed though it is not reflected outside of the function.
The reason is when are sending data to the function the new copy of the same variable is being created within the calling function and the modification is happening to this local variable, it's not effecting the original variable. This is the example of Call by Value in JavaScript.
Call by Reference in JavaScript
Now, in this section, we will understand the concept of "Call by Reference". The theory is, when we want to implement a Call by Reference, we need to send the reference of the object. In C and C++ we use the address of ("&") operator and in C# 'ref' to send the address of a variable. In JavaScript, the situation is a little different. We need to send the actual object as a function argument to send the reference of an object. Try to understand the following code.
  1. <!DOCTYPE html>  
  2. <html xmlns="">  
  3. <head>  
  4. </head>  
  5. <body>  
  6.     <script>   
  7.         function testFun(person) {  
  8.    = 'Ajay';  
  9.             person.surname = 'Joshi';  
  10.         }  
  11.         var person = {  
  12.             name: 'Sourav',  
  13.             surname:'Kayal'  
  14.         };   
  15.         testFun(person);  
  16.         console.log("Name:- " +;  
  17.         console.log("Surname:- " + person.surname);          
  18.     </script>  
  19. </body>  
  20. </html>  
Here is another code for sending an object, basically both are the same and the object creation part is a little different.
  1. <script>   
  2.     function testFun(person) {  
  3. = 'Ajay';  
  4.         person.surname = 'Joshi';  
  5.    }  
  6.    var person = new Object();  
  7. = 'Sourav';  
  8.    person.surname='Kayal'  
  9.    testFun(person);  
  10.    console.log("Name:- " +;  
  11.    console.log("Surname:- " + person.surname);         
  12. </script>  
In both case, the output is the same.
Call by reference in JavaScript
In this example, we are sending a "person" object as a function argument and within the function, we are modifying a value (read property) of the object and we are seeing that that actual object is affected.  So, in an actual scenario, we are not sending a copy of an object, we are sending an actual object as its reference.


Though this is a very small article and nothing new in the concept but the JavaScript developer might love this one.  Hope you have understood the concept. Enjoy JavaScript.