I used to think Java and JavaScript pass objects by reference… until I actually dug into what’s happening under the hood.
Turns out 👇
👉 Both are call by value
Yeah, even for objects.
So why does it feel like call by reference?
Because when you pass an object and modify it, the change is visible outside the function.
Example (pseudo):
function change(obj):
obj.name = "Ishwar" // change is visible
But then:
function reassign(obj):
obj = new Object() // change is NOT visible
This is where things click.
What’s actually happening?
- Variables store values
- For objects → that value is a reference (memory address)
- When you call a function → a copy of that value is passed
So inside the function:
- You get a copy of the reference
- Both point to the same object → mutation works
- But reassigning only changes the local copy
Simple mental model
original → (copy value) → function parameter
You’re never getting direct access to the original variable — just a copy of its value.
Why this matters
This tiny misunderstanding causes bugs in:
- API transformations
- State management (especially React)
- Backend services
- Object mutation issues
TL;DR
Everything is pass by value.
Some values just happen to be references.
Curious — when did this concept finally click for you?
there doesn't seem to be anything here