Contents
We often hear that JavaScript is a “pass by reference” or “pass by value” language, but what does that really mean? In this post, we’ll explore what these terms mean and how they affect the way we write code in JavaScript.
Checkout this video:
Introduction
In JavaScript, everything is an object, and all objects are passed by reference. That includes arrays and functions. However, what exactly does that mean? In this article, we’ll take a closer look at how JavaScript objects are passed around and discuss the implications for your code.
When you pass an object to a function, the function gets a reference to the object. That reference is passed by value. The reference itself is not an object, it’s a value that points to an object. The value of the reference is copied when it’s passed to the function, but the object that the reference points to is not copied.
What is Pass by Reference?
In computer science, pass by reference is a technique for passing arguments to a function in which the function receives an alias for the argument, rather than a copy. Changes to the alias affect the passed argument.
In most programming languages, variables passed as arguments to functions are always passed by value. This means that changes made to the parameter inside the function have no effect on the original variable. However, some languages like C++ and PHP allow you to pass variables by reference, which means that changes made to the parameter inside the function do affect the original variable.
In JavaScript, all function parameters are passed by value. However, objects are passed by reference, so if you make a change to an object parameter inside a function, it will be reflected outside the function.
What is Pass by Value?
In JavaScript, all parameters are passed by value. This means that when a method is invoked, a copy of each of the parameter values is passed to the method. The variables inside the method that receive the parameters are aliases for the corresponding arguments. This is true even if the parameter is an object reference. That reference is copied, so the variable inside the method references a different object than the variable outside of it.
Examples of Pass by Reference
In JavaScript, primitive data types (like strings, numbers, and booleans) are passed to functions by value. That means if you pass a variable to a function, the function gets a copy of the variable’s value. It can’t modify the original variable in the caller.
But when you pass an object to a function, it’s passed by reference. The function gets a reference to (a pointer to) the object, not a copy of the object. The function can modify properties of that object, and those changes are visible outside the function.
Here’s an example:
“`javascript
function addTwo(x) {
x = x + 2; // This change only takes place inside the function
}
var num = 10;
addTwo(num); // Returns 12 but doesn’t change num outside the function
console.log(num); // Prints 10
Examples of Pass by Value
In the simplest terms, this means that when a function is called, a copy of the value is made and given to the function. The original variable is not changed. You can see this in the following example:
“`javascript
function myFunction(x) {
x = 5;
}
let x = 10;
myFunction(x);
console.log(x); // Will output 10
“`
As you can see, even though we changed the value of `x` inside of `myFunction`, it did not change the value of `x` outside of the function. This is because a copy of the value was made and given to `myFunction`.
Conclusion
JavaScript is pass by value, but the value can be a reference to an object. When passing an object to a function, the function gets a reference to that object, not a copy of the object.