DEV Community

Pravin Jadhav
Pravin Jadhav

Posted on • Edited on

Pass by Value vs. Pass by Reference in JavaScript – Simple Explanation

In JavaScript, when you pass variables to a function, they are passed in two different ways:

1️⃣ Pass by Value – A copy of the variable is passed.

2️⃣ Pass by Reference – The original memory address is passed.


📜 Pass by Value (Like Making a Photocopy)

What Happens?

  • When you pass primitive data types (like numbers, strings, booleans) to a function, a copy of the value is made.
  • The original value remains unchanged because the function only works on the copy.

Example 1: Numbers (Pass by Value)

function updateValue(x) {
    x = 100; // Changing the copied value
    console.log("Inside function:", x); // 100
}

let num = 50;
updateValue(num);
console.log("Outside function:", num); // 50 (original remains unchanged)
Enter fullscreen mode Exit fullscreen mode

🔹 Real-Life Example (Photocopy Analogy)

  • Imagine you have a document.
  • You make a photocopy and give it to your friend.
  • Your friend writes on the photocopy, but your original document remains the same.

📜 Pass by Reference (Like Handing Over the Original Document)

What Happens?

  • When you pass objects or arrays, JavaScript passes a reference to the actual memory location, not a copy.
  • The function can directly modify the original object.

Example 2: Objects (Pass by Reference)

function updatePerson(person) {
    person.name = "Alice"; // Changing the original object
}

let personObj = { name: "John" };
updatePerson(personObj);
console.log(personObj.name); // "Alice" (Original object modified)
Enter fullscreen mode Exit fullscreen mode

🔹 Real-Life Example (Original Document Analogy)

  • You have a document, and instead of making a photocopy, you give the original to your friend.
  • Your friend writes on it, and when they return it, your original document is changed.

🚀 Key Differences

Concept Data Types Affected What Happens? Changes Affect the Original?
Pass by Value Number, String, Boolean, Null, Undefined A copy is passed to the function ❌ No
Pass by Reference Object, Array, Function A reference (memory address) is passed ✅ Yes

📌 Example 3: Arrays (Also Pass by Reference)

function modifyArray(arr) {
    arr.push(4); // Modifies the original array
}

let numbers = [1, 2, 3];
modifyArray(numbers);
console.log(numbers); // [1, 2, 3, 4] (Original array is changed)
Enter fullscreen mode Exit fullscreen mode

💡 Even though arrays look like primitive data types, they are objects in JavaScript, so they are passed by reference!


❗ Important Notes

  • Primitive types (numbers, strings, booleans, etc.) are pass by value, so changes inside functions don’t affect the original.
  • Objects and arrays are pass by reference, so changes inside functions affect the original.
  • If you want to avoid modifying the original object, you can create a copy using the spread operator (...) or Object.assign().
function safeUpdate(obj) {
    let newObj = { ...obj }; // Creates a new copy
    newObj.name = "Alice";
    return newObj;
}

let person = { name: "John" };
let updatedPerson = safeUpdate(person);
console.log(person.name); // "John" (Original is safe)
console.log(updatedPerson.name); // "Alice" (Only the copy is changed)
Enter fullscreen mode Exit fullscreen mode

🎯 Final Summary

Pass by Value = A copy is passed (original stays the same).

Pass by Reference = A reference (memory location) is passed (original is modified).

Objects & Arrays are always passed by reference, unless explicitly cloned.

Playwright CLI Flags Tutorial

5 Playwright CLI Flags That Will Transform Your Testing Workflow

  • --last-failed: Zero in on just the tests that failed in your previous run
  • --only-changed: Test only the spec files you've modified in git
  • --repeat-each: Run tests multiple times to catch flaky behavior before it reaches production
  • --forbid-only: Prevent accidental test.only commits from breaking your CI pipeline
  • --ui --headed --workers 1: Debug visually with browser windows and sequential test execution

Learn how these powerful command-line options can save you time, strengthen your test suite, and streamline your Playwright testing experience. Practical examples included!

Watch Video 📹️

Top comments (0)

The best way to debug slow web pages cover image

The best way to debug slow web pages

Tools like Page Speed Insights and Google Lighthouse are great for providing advice for front end performance issues. But what these tools can’t do, is evaluate performance across your entire stack of distributed services and applications.

Watch video

👋 Kindness is contagious

If you found this post useful, please drop a ❤️ or a friendly comment!

Okay.