Explain javascript pass by reference in javascript?

JavascriptFront End TechnologyObject Oriented Programming

In JavaScript there are two methods one is pass by value and the other is pass by reference.

The usage of both depends upon the primitives and objects of JavaScript. JavaScript primitives are undefined, null, Boolean, string, and numbers. So, in JavaScript primitive values are passed by value.

And objects and functions are passed by reference in JavaScript.

JavaScript pass by Reference

In this the address of the variables are directly passed as arguments to the function when it is called. If the argument which is inside the function is changed, then the value of the variable passed from outside the function gets changed.

In JavaScript objects, functions and arrays follows pass by reference function calls.

Example 1

This example can be used to understand how the pass by reference is used to call the function.

let i = 0; function increment(x) { x = x + 1 console.log(x) } increment(i) console.log(i)

Example 2

Following is another example −

function passByRef(Obj) { console.log("The value inside the function passByRef is:"); Obj.a = 147; console.log(Obj); } let Obj = {a:100}; console.log("The value of variable before the function call passByRef:"); console.log(Obj); passByRef(Obj) console.log("The value of variable after the function call passBYRef:"); console.log(Obj);

This is the output displayed in the console for calling a function by pass by reference −

Passing an object as reference

When you pass an object and update that object's reference in the function's context, that won't affect the object. But if you mutate the object's internals, that'll reflect in the object.

Example 1

In this example we are passing an object as a reference to a function −

let obj = {'foo': 'bar'}; function updateRef(x) { // x's local ref gets updates, doesn't affect obj x = {} } function addHelloWorld(y) { // Add new prop to object referenced by y y['hello'] = 'world'; } console.log(obj) updateRef(obj) console.log(obj) addHelloWorld(obj) console.log(obj)

Note that the object reference change did not affect the object. The local variable x was reassigned. In second function however, y was referencing the obj object and mutated the internal state of that object. This caused the change in the original object.

Example 2

Let us see another example for this −

let employee = { name: 'Abdul', salary: 25000, } function salaryHike(obj) { obj.salary += 10000 } console.log("This is the value of variable before the function call by passByRef: ",employee) salaryHike(employee) console.log("This is value inside the function: ",employee) console.log("This is the value of variable after the function call by passByRef: ",employee) console.log("This is the value of variable after the function call by passByRef: ",employee)

This is the output displayed in the console for calling a function by pass by reference.

In the above example, salaryHike() is a function which takes the object as the argument and changes the salary.

Employee is an object which has two properties, name and salary of the employee.

When this object is passed to the function changeSalary(), it does the operation and returns the object with change in salary. Now in the memory, both the object after the function call and before the function call will point to the same object. The object will have the value returned by the function salaryHike(). So, by this the value of the variable gets changed and the new value is displayed whenever used after the function call.

If an object or array is passed to a function by using pass by reference, then the value of the arguments will change.

raja
Updated on 06-Oct-2022 12:48:24

Advertisements