pass by reference in javascript code example
Example 1: javascript pass by reference
function add(a, b, output) {
output.out = a + b;
}
var output = {};
add(5, 3, output);
console.log(output); //output: {out: 8}
Example 2: javascript pass by value
function func(obj) {
obj = JSON.parse(JSON.stringify(obj)); //If too slow, replace with other method of deep cloning
obj.a += 10;
return obj.a;
}
var myObj = {a: 5};
func(myObj); //Returns 15 and myObj.a is still 5