JavaScript

Understanding JavaScript Pass-By-Value

In JavaScript, data is passed either by reference or by values. The primary conflict is that the pass-by-value makes a copy of your data while the pass-by-reference doesn’t create a copy. In JavaScript, the arrays and objects will always be passed by reference while anything else such as float, strings, int, etc. will be passed by value.

So, all in all, we can say that pass-by-value means passing a copy of the data while pass-by-reference means passing the actual reference of the variable in the memory.

In this write-up we will understand the below-listed aspects of pass-by-value in JavaScript:

So, let’s get started!

What is Pass-by-value and how does it work in JavaScript?

Let’s consider the below code snippet to understand what exactly pass-by-value is and how pass-by-value works in JavaScript:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function examplePBV(number1, number2) {
number1 = 50;
number2 = 100;
console.log("Variable's value within examplePBV Method");
console.log(" number1 = " + number1 +" number2 = " +number2);
}

let number1 = 172;
let number2 = 72;

console.log("Variable's value Before Calling examplePBV Method");
console.log(" number1 = " + number1 +" number2 = " +number2);
examplePBV(number1, number2);
console.log("Variable's value after Calling examplePBV Method");
console.log(" number1 = " + number1 +" number2 = " +number2);

In this example program, we performed the following tasks:

  • Created a couple of variables and a function examplePBR().
  • Printed the value of the variables before calling the method, within the method, and after calling the method.

As we have mentioned earlier, pass-by-value creates a copy of data therefore, it doesn’t change the original values of the variables. This is how the pass-by-value works in JavaScript.

What is Pass-by-reference how does it work in JavaScript?

This section will consider a couple of examples to show what exactly pass-by-reference is and how it works in JavaScript?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function examplePBR(obj) {
  obj.value = 172;
  console.log("Object value inside examplePBR function:", obj);
  }
 
  var obj = {
       value: 72
  };

 console.log("Object's value Before Calling examplePBR Method");
 console.log(obj);
 examplePBR(obj);
 console.log("Object's value after Calling examplePBR Method");
 console.log(obj);

In this example program, we performed the following tasks:

  • Created an object “obj”, a function examplePBR().
  • Printed the value of the object before calling the method, within the method, and after calling the method.

As we have mentioned earlier, pass-by-reference doesn’t create a copy of data, therefore, modifications made in the examplePBR() function affect the original value.

Conclusion

In JavaScript, data can be passed in two ways i.e., by reference or by values. In JavaScript, pass-by-value creates the copy of data, on the other hand, pass-by-reference doesn’t create any copy. This post considered some appropriate examples to explain how pass-by-value and pass-by-reference work in JavaScript.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.