[Javascript] Pass By Value And Pass By Reference In JavaScript

Raghuraman Kesavan
Aug 23, 2017 · 2 min read

Pass by Value:

function callByValue(varOne, varTwo) { 
console.log("Inside Call by Value Method");
varOne = 100;
varTwo = 200;
console.log("varOne =" + varOne +"varTwo =" +varTwo);
}
let varOne = 10;
let varTwo = 20;
console.log("Before Call by Value Method");
console.log("varOne =" + varOne +"varTwo =" +varTwo);
callByValue(varOne, varTwo) console.log("After Call by Value Method");
console.log("varOne =" + varOne +"varTwo =" +varTwo);

output will be :
---------------
Before Call by Value Method
varOne =10 varTwo =20
Inside Call by Value Method
varOne =100 varTwo =200
After Call by Value Method
varOne =10 varTwo =20

Pass by Reference:

function callByReference(varObj) { 
console.log("Inside Call by Reference Method");
varObj.a = 100;
console.log(varObj);
}
let varObj = {a:1};
console.log("Before Call by Reference Method");
console.log(varObj);
callByReference(varObj) console.log("After Call by Reference Method");
console.log(varObj);
output will be :
---------------
Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}

NodeSimplified

Share your knowledge!

Raghuraman Kesavan

Written by

Blogger@nodesimplified.com

NodeSimplified

Share your knowledge!