Pass a variable by reference in JavaScript
This post will discuss how to pass a variable by reference in JavaScript.
There is no built-in way to pass a variable by reference in JavaScript, as JavaScript always passes arguments by value, not by reference. However, there are some possible workarounds to achieve a similar effect, depending on our use case and preference. Here are some of the common functions:
1. Using an object property
We can wrap the variable in an object and pass the object as an argument to a function. Then, we can modify the object property inside the function, and the change will be reflected outside the function as well. This is because objects are passed by value of their reference, meaning that the function receives a copy of the reference to the same object in memory. Here’s an example of how we can achieve this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// Define a function that increments an object property function increment(obj) { obj.value++; } // Create an object with an integer property let myObj = { value: 10 }; // Pass the object to the function increment(myObj); // Log the updated object console.log(myObj); // { value: 11 } |
2. Using a closure
We can create a function that returns another function that has access to a local variable in its outer scope. This way, we can create a private variable that can only be modified by the inner function, and return the inner function as a reference to the variable. Here’s an example of how we can achieve this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// Define a function that returns another function with access to a local variable function createCounter() { // Create a local variable let count = 0; // Return a function that increments and returns the local variable return function() { count++; return count; }; } // Create a reference to the local variable by calling the outer function let counter = createCounter(); // Call the inner function multiple times and log the result console.log(counter()); // 1 console.log(counter()); // 2 console.log(counter()); // 3 |
3. Using a generator
A third way is to use a generator, which is a special kind of function that can yield multiple values and resume its execution. We can create a generator that takes an integer as an argument and yields incremented values when called. Here’s an example of how we can achieve this:
|
1 2 3 4 5 6 7 8 9 10 11 |
function* incrementer(num) { while (true) { num++; yield num; } } let num = 5; let increment = incrementer(num); console.log(increment.next().value); // 6 console.log(increment.next().value); // 7 |
4. Using an array
Finally, we can put the variable in an array and pass the array as an argument. Then, we can modify the array element inside the function and it will affect the original array. For example:
|
1 2 3 4 5 6 7 |
function increment(arr) { arr[0]++; // Increment value of the first array element } let num = [5]; // An array with an integer element increment(num); // Pass the array as an argument console.log(num[0]); // 6 |
That’s all about passing a variable by reference in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)