Open In App

Pass by Value and Pass by Reference in Javascript

Last Updated : 22 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

JavaScript handles variables in different ways when passing them to functions. Variables in JavaScript can either be passed by value or passed by reference, depending on the type of data they hold.

“Pass by Value” in JavaScript

When a variable is passed by value, a copy of the actual value is passed to the function. Any changes made to the parameter inside the function do not affect the original variable.

In JavaScript, primitive data types are passed by value. These include:

JavaScript
let n = 10;

function modify(x) {
    x = 20;
    console.log("Inside function: ", x);
}

modify(n);
console.log("Outside function: ", n); 

Output
Inside function:  20
Outside function:  10

In this example

  • The value of n (10) is copied to x.
  • Modifying x inside the function does not affect n because x is a separate copy.

“Pass by Reference” in JavaScript

When a variable is passed by reference, a reference to the actual data is passed. This means changes made to the parameter inside the function affect the original variable.

In JavaScript, non-primitive data types (such as objects and arrays) are passed by reference.

JavaScript
let obj = { name: "Ravi" };

function modify(o) {
    o.name = "Arun";
    console.log("Inside function: ", o.name);
}

modify(obj);
console.log("Outside function: ", obj.name);

Output
Inside function:  Arun
Outside function:  Arun

In this example the obj reference is passed to the function, allowing changes to the original object.

Difference between Pass by Value and Pass by Reference

FeaturePass by ValuePass by Reference
Applies ToPrimitive data typesNon-primitive data types (objects, arrays)
Data CopyCreates a copy of the actual valuePasses a reference to the original data
Effect of ChangesChanges inside the function do not affect the original variableChanges inside the function affect the original variable
Use CasesNumbers, strings, booleans, etc.Arrays, objects, functions

Important Clarification: Does JavaScript Truly Support “Pass by Reference”?

JavaScript does not have “true pass by reference.” Instead:

  • Primitive types (like numbers, strings, booleans) are always passed by value—a copy of the value is passed.
  • For objects and arrays, JavaScript passes the reference value, which is essentially a pointer to the memory location of the data. This allows modifications to the object or array but does not allow reassigning the original variable within the function.
JavaScript
let obj = { name: "Ravi" };

function reassignReference(o) {
    // Reassigning the reference
    o = { name: "Arun" }; 
    console.log("Inside function: ", o.name); 
}

reassignReference(obj);
console.log("Outside function: ", obj.name);

Output
Inside function:  Arun
Outside function:  Ravi

In this example

  • The function receives a reference to the original object.
  • However, reassigning o creates a new object, leaving the original object untouched.

Thus, JavaScript passes reference values for objects but not “by reference” as in languages like C++.

Common Pitfalls

1. Unexpected Side Effects

When working with objects or arrays, be cautious of unintended changes to the original data.

JavaScript
let config = { theme: "dark" };

function updateTheme(c) {
    c.theme = "light";
}

updateTheme(config);
console.log(config.theme); 

2. Cloning to Avoid Reference Issues

To avoid modifying the original object, create a shallow or deep copy.

Shallow Copy

JavaScript
let original = { name: "Ravi" };
let copy = { ...original };
copy.name = "Arun";
console.log(original.name);

Output
Ravi

Deep Copy

JavaScript
let original = { name: "Ravi", details: { age: 25 } };
let deepCopy = JSON.parse(JSON.stringify(original));
deepCopy.details.age = 30;
console.log(original.details.age);

Output
25

Master DSA with JavaScript in just 90 days. Explore core DSA concepts, refine your coding skills, and tackle real-world challenges. Take on the Three 90 Challenge—complete 90% of the course in 90 days and earn a 90% refund as a reward for your commitment!


Next Article

Similar Reads

Pass by Reference in JavaScript ?
Contrary to common belief, JavaScript is not a pass-by-reference language but Instead, it is a pass-by-value for all its variable passing. In JavaScript, when an object is passed as an argument to a function a reference to the object is passed not a direct copy of the object itself. The modifications made to the object's properties within the funct
1 min read
How to Reference a Specific Object to Pass Down as Properties in ReactJS ?
ReactJS, a widely used JavaScript library, employs Props, enabling the passing of data from parent to child components. Props are read-only, facilitating data access by child components via 'this.props.' The ReactJS library has two components: Functional ComponentClass ComponentSteps to Create the React Application:Step-1: Create a React Applicatio
3 min read
How to pass PHP Variables by reference ?
By default, PHP variables are passed by value as the function arguments in PHP. When variables in PHP is passed by value, the scope of the variable defined at function level bound within the scope of function. Changing either of the variables doesn't have any effect on either of the variables. Example: php <?php // Function used for assigning ne
2 min read
Pass by Value in JavaScript
In JavaScript, the concept of "pass by value" explains how arguments are passed to functions. When primitive data types (like numbers, strings, and booleans) are passed as function arguments, the actual value is copied into the parameter inside the function. Changes made to the parameter inside the function do not affect the original value outside
1 min read
What is Pass by Value in JavaScript ?
In JavaScript, pass-by value refers to the mechanism by which arguments are passed to functions. When primitive data types (like numbers, strings, or booleans) are passed as arguments, a copy of the actual value is passed to the function. Changes made to the parameter inside the function do not affect the original value outside the function. This b
1 min read
How to pass value to execute multiple conditions in JavaScript ?
In this article, we will try to understand how we can pass certain values in multiple conditions in JavaScript with the help of certain coding examples. The term, multiple conditionals, actually refer to more than one conditionals in number. This would eventually include the usage of "if-elseif-else" or switch statement. We can also use special fun
3 min read
Primitive and Reference value in JavaScript
In JavaScript, a variable may store two types of values, Primitive values or Reference values. This article will describe and help to compare both these types of values. Primitive value: JavaScript provides six types of primitive values that include Number, String, Boolean, Undefined, Symbol, and BigInt. The size of Primitive values are fixed, ther
2 min read
Call by Value Vs Call by Reference in JavaScript
Call by Value: In this method, values of actual parameters are copied to the function’s formal parameters, and the two types of parameters are stored in different memory locations. So any changes made inside functions are not reflected in the actual parameters of the caller. Suppose there is a variable named “a”. Now, we store a primitive value(boo
3 min read
How to select onchange pass option value in angular 6 ?
In this article, we will see how to select onchange pass option value in angular 6, along with knowing the different techniques for retrieving the value of the user-selected item in a dropdown list & will understand their implementation through the illustration. The Dropdown is a commonly used element in HTML forms that facilitates the users to
5 min read
How to pass value between Screens in React Native ?
React Native is use to create cross-platform applications. One of the biggest drawbacks of React Native is that it doesn't come with any built-in navigation system. We have to use other libraries to implement navigation in React Native. React Navigation library is widely used with React Native to implement various types of navigation systems.With t
4 min read
  翻译: