A JavaScript variable stores its own reference to a value, so setting `person` to `null` doesn’t erase an object held in `members`. The array still contains Lydia’s object. Understanding how assignment and references work makes everyday code easier to reason about—and helps prevent surprising changes to shared data.

Multiple Choice

After setting person to null, what will console.log(members) output if members was initially equal to [{ name: 'Lydia' }]?

When the variable `members` is initially set to `[{ name: 'Lydia' }]`, it represents an array containing a single object. If you then set a different variable, `person`, to null, this action does not affect the `members` array itself. The `members` array continues to hold its original value. Therefore, when you execute `console.log(members)`, it outputs the exact state of the `members` array at that moment, which is still `[{ name: 'Lydia' }]`. The assignment of null to a different variable (`person`) is completely independent and does not alter the contents of the `members` array. Thus, the output is the unchanged structure of the original array containing the object with Lydia's name.

In JavaScript, setting one variable to null doesn’t erase data held by another variable. That sounds simple, but it matters whenever code handles arrays, objects, or records—especially in Salesforce interfaces, where a small change in state can affect what a Lightning Web Component displays.

Suppose members refers to an array containing one object:


const members = [{ name: 'Lydia' }];

let person = members[0];

person = null;

console.log(members);

The output is still:


[{ name: 'Lydia' }]

The key is that person = null changes what the variable person refers to. It doesn’t reach into the array and remove or rewrite its contents. To see why, it helps to look at how JavaScript treats variables, objects, and references.

Variables hold different kinds of values

JavaScript values are commonly grouped into two broad categories: primitive values and objects. Strings, numbers, booleans, null, and undefined are primitives. Arrays and ordinary objects are objects.

A variable assigned a primitive value holds that value directly. For example:


let status = 'active';

status = 'inactive';

The second assignment gives status a new string value. The original string hasn’t been changed; the variable now holds something else.

Objects behave differently in one important way. A variable assigned an object refers to that object. You can picture the variable as a label pointing to a place in memory:


const member = { name: 'Lydia' };

Here, member refers to the object. Arrays work the same way because an array is an object too:


const members = [{ name: 'Lydia' }];

The variable members refers to an array, and the array contains a reference to an object. The object has a name property whose value is the string 'Lydia'.

That structure matters. The array and its contents are not all one flat value. They’re connected, but they’re still distinct pieces of data.

What does assigning null actually do?

Consider this version:


const members = [{ name: 'Lydia' }];

let person = members[0];

person = null;

At first, members[0] and person both refer to the same object. There are now two ways to reach it: through the first array element and through the variable person.

When the code assigns null to person, it changes only that variable’s reference. The array still holds its reference to the object. So this remains true:


console.log(members[0].name); // 'Lydia'

console.log(person);          // null

A useful analogy is a contact card in a shared address book. Two people may each have a link to the same card. If one person removes the link from their own list, the card doesn’t disappear from the address book—or from the other person’s list.

JavaScript references aren’t literal links you can see, of course. But the analogy captures the difference between changing a variable and changing the object it refers to.

Reassigning a reference isn’t the same as changing an object

These two snippets look related, but they do different things:


person = null;

person.name = 'Maya';

The first assigns a new value to the variable. The second tries to change a property on the object that person refers to.

If person currently refers to an object, this property assignment changes that object:


const members = [{ name: 'Lydia' }];

let person = members[0];

person.name = 'Maya';

console.log(members);

// [{ name: 'Maya' }]

Because both person and members[0] refer to the same object, changing a property through one reference is visible through the other. The object itself has changed.

But assigning null doesn’t mutate the object. It simply makes person refer to a different value. This distinction—reassignment versus mutation—turns up everywhere in JavaScript, from browser code to Salesforce components.

Arrays keep their contents until code changes them

An array doesn’t automatically change just because a separate variable changes. To remove an entry, code must act on the array itself:


const members = [{ name: 'Lydia' }];

members.pop();

console.log(members); // []

Or code could replace an element:


const members = [{ name: 'Lydia' }];

members[0] = null;

console.log(members); // [null]

Those statements explicitly modify the array. By contrast, this statement does not:


let person = members[0];

person = null;

It only changes person.

This is why it’s worth reading assignments closely. Ask: which variable appears on the left side of =? If the left side is person, that assignment affects person. If the left side is members[0], it affects the array element. If the left side is person.name, it affects a property on the referenced object—assuming person still refers to an object.

A small detail about const

The example uses const for members and let for person. That choice is deliberate:


const members = [{ name: 'Lydia' }];

let person = members[0];

person = null; // allowed

const prevents reassignment of the members variable. It does not freeze the array or make the objects inside it immutable. This is allowed:


members[0].name = 'Maya';

But this is not:


members = [];

The variable members must keep referring to the same array. The contents of that array can still change unless the code uses additional safeguards, such as Object.freeze().

That difference can surprise developers. “Constant” sounds like “unchangeable,” yet with objects it means the reference can’t be reassigned—not that every nested value is locked.

Why this matters in Salesforce JavaScript

Salesforce developers work with JavaScript in Lightning Web Components, where arrays and objects often represent records, component state, or data returned from an Apex method. Understanding references helps explain why a displayed list may change—or why it doesn’t.

For example, a component might store a list of contacts:


contacts = [

{ id: '1', name: 'Lydia' }

];

A local variable can point to one contact:


let selectedContact = this.contacts[0];

selectedContact = null;

That assignment clears the local variable, but it doesn’t remove the contact from this.contacts. If the interface should no longer show the contact, the component must update the array or derive a new list.

The same idea matters when working with event handlers, child components, and data passed between parts of an interface. A reference can be shared, so changing an object’s properties may be visible elsewhere. Reassigning a local variable, on the other hand, usually affects only that variable. In component code, keeping those actions distinct makes state changes easier to follow and reduces unexpected UI behavior.

A simple way to trace the result

When code involving arrays and objects feels confusing, trace it one assignment at a time:

  1. Identify each variable and the value it currently refers to.

  2. Note whether the code reassigns a variable or changes an object or array.

  3. Check whether two variables refer to the same object.

  4. Look at the exact structure being passed to console.log.

For the example, members still refers to its original array. That array still contains its original object. The separate variable person now holds null. So logging members shows the array with Lydia’s name.

It’s a small point, but a useful one: a reference can move without moving the thing it once pointed to. Once that distinction clicks, many everyday JavaScript behaviors become much easier to read.