Is JavaScript’s Object Assignment Similar to C Pointers?

0
1
Asked By MellowCedar27 On

I'm learning both C and JavaScript and I'm trying to connect the concepts. In C, a pointer is a value that stores the address of another object, while the pointer variable itself also has an address. In JavaScript, I understand that primitive values such as numbers, strings, booleans, null, undefined, BigInts, and symbols behave as values, while objects and arrays seem to be shared when assigned to another variable.

For example:

let person = { name: "Joanna", age: 18 };
let otherPerson = person;

My current mental model is that `person` contains some kind of reference to the object, and assigning it to `otherPerson` gives both variables a reference to the same object. Is this a reasonable analogy to a pointer in C? Are JavaScript objects actually stored through memory addresses in this way, or is that an implementation detail?

I'm especially trying to understand the difference between copying a value, sharing an object, mutating an object, and reassigning a variable. I'm asking about JavaScript's assignment and argument-passing behavior rather than formal pass-by-reference parameters.

3 Answers

Answered By QuartzMango8 On

The behavior you’re describing is real, but “copy by reference” is a misleading phrase for JavaScript. A more accurate description is that JavaScript is pass-by-value, and an object value can be a reference to an object.

For example:

let a = { value: 1 };
let b = a;

Both variables now contain the same object reference, so `a.value = 2` is visible through `b`. However, assigning a new object to `a` does not change `b`:

a = { value: 3 };

Now `a` and `b` refer to different objects, while `b.value` is still `2`. The reference itself was copied; the object was not.

NeonBirch41 -

That distinction helped. Mutation changes the shared object, but reassignment only changes which object that particular variable refers to.

Answered By CrispWillow19 On

Function arguments follow the same rule. The argument receives a copy of the value. If that value refers to an object, the function can mutate the shared object, but it cannot reassign the caller’s variable.

function change(item) {
item.name = "New name"; // affects the original object
item = { name: "Another name" }; // only changes the local parameter
}

let person = { name: "Joanna" };
change(person);
console.log(person.name); // "New name"

So “pass by sharing” is another useful description: the reference value is copied, and both the caller and function parameter initially refer to the same object.

AmberLynx52 -

This also explains why JavaScript doesn’t have ordinary C-style pass-by-reference parameters. To replace the caller’s variable, the function would need to return a new value and the caller would need to assign it.

Answered By TidyFalcon6 On

A C pointer is a useful analogy as long as you don’t take it too literally. In C, you can inspect and manipulate addresses, perform pointer arithmetic, choose how to dereference a pointer, and control memory manually. JavaScript does not expose those operations.

JavaScript engines may internally use pointers, tagged values, stack storage, heap storage, or other optimizations. Those details can differ between engines and can change without affecting the language behavior. It’s therefore safer to think in terms of values and object identity rather than assuming a specific memory layout.

The reliable rule is: primitives behave as independent values, while assigning an object or array copies a reference to the same object.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.