Pass by Value vs Pass by Reference (and What Python, JavaScript and Java Actually Do)
Why a function can change your list but not reassign your variable, what "pass by object reference" means in Python and JavaScript, why Java is always pass-by-value, and the mutable default argument trap.
By Software Side Hustles Editorial Team · Published · Updated · 4 min read
Quick answer: Python, JavaScript and Java all pass arguments by value — but for objects, the value being copied is a reference to the object. So a function can modify an object you pass in (append to a list, set a field), but it cannot replace your variable with a different object. This model is often called "pass by sharing" or "pass by object reference". True pass-by-reference, where the function can rebind the caller's variable, exists in C++ (&) and C# (ref).
The two behaviours people confuse
def add_item(items): items.append(4) # mutates the object the caller passed
def replace(items): items = [0] # rebinds the local name only
nums = [1, 2, 3]add_item(nums)print(nums) # [1, 2, 3, 4] — changedreplace(nums)print(nums) # [1, 2, 3, 4] — unchangedInside the function, items is a new name pointing at the same list as nums. Calling a method on it reaches the shared list. Assigning to it just points the local name somewhere else; the caller's nums is untouched.
The same experiment gives the same results in JavaScript and Java:
function addItem(items) { items.push(4); }function replace(items) { items = [0]; }
const nums = [1, 2, 3];addItem(nums); // nums is [1, 2, 3, 4]replace(nums); // nums is still [1, 2, 3, 4]static void addItem(List<Integer> items) { items.add(4); }static void replace(List<Integer> items) { items = List.of(0); }Why numbers and strings never seem to change
def increment(n): n += 1
x = 5increment(x)print(x) # 5Integers, floats, strings and tuples are immutable in Python; there is no method that changes them in place. n += 1 creates a new integer and rebinds the local name. Strings and numbers behave the same way in JavaScript and Java (String is immutable in Java). So, for immutable values, passing a reference and passing a copy are indistinguishable — which is why people describe them as "pass by value" and objects as "pass by reference". The underlying mechanism is the same for both.
Java is always pass-by-value
The classic proof is a swap function that does not work:
static void swap(StringBuilder a, StringBuilder b) { StringBuilder tmp = a; a = b; b = tmp; // only the local copies of the references are swapped}After calling swap(x, y), x and y still refer to their original objects. Java copies the reference into the parameter; it never gives the method access to the caller's variable. That is why Java methods return new values, or mutate an object they were given, rather than "returning through" parameters.
Real pass-by-reference: C++ and C#
void increment(int& n) { n += 1; } // & makes n an alias for the caller's variable
int x = 5;increment(x); // x is now 6void Replace(ref List<int> items) { items = new List<int> { 0 }; }Replace(ref nums); // nums now refers to the new listC# also has out parameters (for returning extra values, as in int.TryParse(s, out var n)) and in parameters (read-only references, used for performance with large structs). In C, you get the same effect explicitly by passing a pointer.
Go: values, with built-in references
Go passes everything by value, including structs (copied in full). Slices, maps and channels, however, are small headers containing pointers, so a function receiving a slice can modify its elements. Appending is subtler: if append has to grow the underlying array, the function's slice points to new memory and the caller does not see the new elements. Return the slice from the function (s = add(s, x)) — the same pattern as the built-in append.
The mutable default argument trap (Python)
def add_tag(tag, tags=[]): tags.append(tag) return tags
add_tag("a") # ['a']add_tag("b") # ['a', 'b'] — the same list as last time!Default values are evaluated once, when the function is defined. Every call that uses the default shares that one list. Use None as a sentinel:
def add_tag(tag, tags=None): if tags is None: tags = [] tags.append(tag) return tagsLinters (Pylint's dangerous-default-value, Ruff's B006) flag the bug automatically. JavaScript does not have this problem — default parameters are evaluated on every call.
Designing functions with this in mind
- Do not mutate arguments unless that is the function's job — and when it is, name it clearly (
sort_in_place,appendTo). Callers do not expectcalculate_total(order)to change the order. - Return new values for transformations:
sorted(items)rather thanitems.sort()when the caller's list must be preserved. - Copy at boundaries when you store objects passed in by someone else. See deep copy vs shallow copy.
- Prefer immutable data (tuples, frozen dataclasses, Java records with immutable fields,
Object.freeze) for values shared widely.
Frequently asked questions
Is Python pass by value or pass by reference?
Neither label fits perfectly. Python passes references to objects by value ("pass by object reference"). Mutations are visible to the caller; reassignments are not.
Is JavaScript pass by reference for objects?
No — it is the same model as Python. A function can change an object's properties, but cannot make the caller's variable refer to a different object.
How do I make a function change a variable in Python?
Return the new value and assign it: x = increment(x). For several values, return a tuple.
Does passing a large list to a function copy it?
No. Only the reference is passed, so it is cheap regardless of size.
Where this skill pays off
Our behavioural evaluation lab includes a case where a user pushes back on a correct explanation of list mutability — and the AI caves. Recognising which answer is technically right, and holding to it politely, is part of paid AI evaluation work. Read how software engineers earn money training AI models.