Skip to content

reference

A reference is the value that a variable, property, or array element holds when it points to an object, so several names can lead to one shared object instead of to separate copies.

Every object is reached through a reference, including arrays, functions, and class instances. Primitive values such as numbers and strings are held directly. A const declaration only fixes which reference a name holds, so a constant array can still gain or lose elements.

Assigning an object to a second variable copies the reference, not the object. Both names then reach the same object, and a change made through one shows up through the other. The === operator compares references, so two objects are equal only when they’re the same object, even if their properties match.

Function calls follow the same rule. JavaScript passes every argument by value, and an object’s value is its reference, which is sometimes called pass by sharing. A function that changes a property of an object argument changes the caller’s object, but reassigning the parameter only redirects the local name.

An independent object needs an explicit copy. Spread syntax copies only the top level, so nested objects stay shared, while structuredClone(), a host function that browsers and Node.js both provide, copies them too. ECMA-262 also defines a Reference Record, an internal record for looking up names and properties that’s unrelated to object references.

Example

Say an order form starts from a customer’s saved address, and the customer edits the shipping city on the order:

Language: JavaScript
> const savedAddress = { city: "Lisbon" };
undefined
> const order = { shipping: savedAddress };
undefined

> order.shipping.city = "Porto";
'Porto'
> savedAddress
{ city: 'Porto' }

> order.shipping === savedAddress
true
> savedAddress === { city: "Porto" }
false

The order’s shipping property holds a reference to the saved address, so editing the order also changed the saved address. A new object with identical contents is still a different object, so the last comparison is false.

Say a shopping cart module tries to empty a cart in two ways:

Language: JavaScript
> const items = ["mug", "tea"];
undefined

> function clearByReassigning(list) {
…   list = [];
… }
undefined
> clearByReassigning(items);
undefined
> items
[ 'mug', 'tea' ]

> function clearInPlace(list) {
…   list.length = 0;
… }
undefined
> clearInPlace(items);
undefined
> items
[]

The first function points its local list parameter at a new array and leaves the caller’s array untouched. The second changes the shared array through its reference, so the cart empties even though items is declared with const.

Step through either function to watch where items and list point and which array changes:

Interactive diagram — enable JavaScript to view.
A developer points at a tower where a robotic gripper reorders colored rows, beside a copier duplicating cards and a row frozen in ice.

Course

Copying, Freezing, and Sorting JavaScript Arrays for Pythonistas

Port a five-a-side league's Python table script to JavaScript, and learn how to copy, freeze, and sort arrays and records without changing data you meant to keep.

intermediate js py

For additional information on related topics, take a look at the following resources:

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Nov. 17, 2026