Functions - JavaScript Pass By Value
Posted by Superadmin on May 05 2023 04:54:41

JavaScript Pass By Value

By Asapu HarikaAsapu Harika
  

JavaScript Pass By Value

Introduction to JavaScript Pass By Value

One of the most confusing and interesting features in programming is to differentiate whether a variable is passed by Value or passed by Reference. But in JavaScript, we need not struggle as the only pass by Value concept is used. JavaScript Pass by Value is the function being called by the directly passing value of a variable as arguments. In other words, Change of arguments inside function won’t affect variables that are being passed from outside of the function. JavaScript being Object-Oriented which means most of the parts of JavaScript are Objects. So here comes the concept of Data Types too, Primitive and Complex data types will also play a role in JavaScript Pass by Value.

Syntax:

function functionName(arg1) {
// code of the function
}
// call to the function
functionName(arg2);

So here, functionName is the function which has its arguments, after the function definition, we call the functionName using another argument, which will automatically replace the defined argument.

JavaScript will copy the value of variables that we pass to a function as local variables. Any change we make to local variables inside function won’t affect the argument which is passed.

We have 2 data types, Primitive ( Immutable data types )and Complex data types.

So here comes the interesting part is, Only Primitive types can be Passed by Value but not Complex types, which can be passed by reference only. Technically, Pass by Reference is present in JavaScript. When variables hold an object, an array of a function which is complex types passes by Reference comes into picture where variable holds the reference or address of the object.

For now, we need not consider passing by Reference concept and get deeper into Pass by Value concept.

Examples to Implement JavaScript Pass By Value

Below are the examples mentioned :

Example #1

Code:

<!DOCTYPE html>
<html>
<body>
<script>
var employeeName1 = "Jack";
var employeeName2 = employeeName1;
employeeName1 = "Jane";
document.write(employeeName1 + "</br>");
document.write(employeeName2);
</script>
</body>
</html>

Output:

JavaScript Pass By Value - 1

Here, it’s a straightforward result, we are using only = operator here to display pass by value concept.

Example #2

Code:

<!DOCTYPE html>
<html>
<body>
<script>
let x = 10;
let samplechange = (value) => {
value = 20
}
samplechange(x);
document.write(x);
</script>
</body>
</html>

Output:

JavaScript Pass By Value - 2

Example #3

Code:

<!DOCTYPE html>
<html>
<head>
<title>Pass by Value</title>
</head>
<body>
<h2>Pass by Value in JavaScript</h2>
<script>
var y = 30;
function sum(x) {
x = x + x;
}
sum(y);
document.write(y);
</script>
</body>
</html>

Output:

JavaScript Pass By Value - 3

Let me explain How Pass by Value works with the above example,

Example #4

Before callByValue, Inside function and after callByValue change in the value of variables

Code:

<!DOCTYPE html>
<html>
<head>
<title>Pass by Value</title>
</head>
<body>
<h2>Pass by Value in JavaScript</h2>
<script>
function callValue(var1, var2) {
document.write("Inside Call by Value Method");
var1 = "Chaitanya";
var2 = "CPG";
document.write(" var1 = " + var1 +" var2 = " +var2 + "</br>");
}
let var1 = "Karthick";
let var2 = "IBM";
document.write("Before Call by Value Method");
document.write(" var1 = " + var1 +" var2 = " +var2 + "</br>");
callValue(var1, var2)
document.write("After Call by Value Method");
document.write(" var1 = " + var1 +" var2 = " +var2 + "</br>");
</script>
</body>
</html>

Output:

value method

Example #5

Code:

<!DOCTYPE html>
<html>
<head>
<title>Pass by Value</title>
</head>
<body>
<h2>Pass by Value in JavaScript</h2>
<script>
let var1 = 100;
function updateValue(var1){
var1 = var1 * 4;
document.write("Updated Value is: ", var1 + "</br>");
}
updateValue(var1);
document.write("Original Value is: ", var1);
</script>
</body>
</html>

Output:

updated

Advantages of Pass By Value

Below are the advantages :

Conclusion

We have seen how this feature works in JavaScript considering Primitive and Complex data types. Illustrated with few examples which will help you to explore further and also had a look into advantages of pass by Value. Given a brief explanation of the examples will leave you with a good knowledge of how to pass by Value is used. You people can even explore Pass by Reference in JavaScript which is for complex data types.