HomeToolsAbout

Pass by Value vs Pass by Reference

Pass by value

Copy is made in memory.

New memory allocation is done.

  • all the changes are made to the new value/memory location.
let num1 = 70; let num2 = num1; console.log(num1) // 70 console.log(num2) // 70 num1 = 40 console.log(num1) // 40 console.log(num2) // 70

Pass by reference

Does NOT create a new space in memory.

  • passes the reference/address pointer of the original memory.
const obj1 = {website: "Scaler Academy"} const obj2 = obj1; console.log(obj1); // {website: "Scaler Academy"} console.log(obj2); // {website: "Scaler Academy"} obj1.website = "Scaler Topics" console.log(obj1); // {website: "Scaler Topics"} console.log(obj2); // {website: "Scaler Topics"}
AboutContact