FAQ: Objects - Pass By Reference


#1

This community-built FAQ covers the “Pass By Reference” exercise from the lesson “Objects”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise Pass By Reference

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


#2

What’s the difference between a spaceship object and the spaceship variable. In lesson 8 of objects, the lesson talks about a function that receives a parameter of spaceship, but apparently the obj is a pointer to the spaceship object, not the spaceship variable, causing the function to be useless.


#3

A variable is not a value and an object is not a pointer, what you’re saying doesn’t add up.
A variable is a name that can hold on to a reference. References are hidden from you, you can’t interact with them. It just means that variables are not storage, instead they know where the value is stored.
An object is a value.

You can think of variables as names pointing into memory. When you assign a variable you are changing where it points. When you read from a variable you read the object, the dereferencing is done by the language, you can’t see the references themselves.


#4

I’m not grasping the distinction taught in this lesson (function does change object property by mutating the object permanently BUT reassignment of variable does not work in the same way within a function).

Sir, you stated that “variables are not storage, instead they know where the value is stored”. That confuses me because I am new to JS and in the introductory lessons of this course (in the second chapter: “Variables”), we were taught that variables:
-store information
-are a container for a value
-label and store data in memory
-hold reusable data in a program and associate it with a name

That lesson appears to conflict with your reply here (and it seems you have a firm grasp of JS), so I am apparently misunderstanding something. Would you be willing to expand a bit to help me understand?


#5

A variable is like a piece of paper where you have written a street address.

It doesn’t store the house, but it has the information for finding it. You can copy it without building a house. The copy will refer to the same house.
Assignment writes a new address to the paper. It does not cause a house to be built.

You can’t see or interact with that address, and dereferencing it happens automatically, this is a big reason why it’s a bit difficult to explain and to understand - can’t touch it can’t see it. But you can observe effects of it, for example by having two variables refer to the same value, and modifying the value:

let a = []
let b = a       // a and b refer to the same thing
b.push(3)       // modify from b
console.log(a)  // observe effect from a

Anything that refers to a value behaves this same way, and this is generally very convenient behaviour. The language runtime deals with the backing memory, we don’t care, we almost never wish for such fine-grained control.


#6

Thank you for the input. It is greatly appreciated!


#7

Again I’m not a fan of using concise arrow functions until I know what I’m doing writing the same thing as anormal function first. This problem assumes I know what I’m doing already and doesnt explain what to do. The “hint” is just the answer, and to hit “give me the solution” just gives me the same answer. Also for some reason no matter what I write as the solution it wont let me pass.


#8


why does the first example the function can change the object spaceship permanently? Is that because the function accesses the object property directly? which is obj.color = ‘glorious gold’? We don’t call this reassignment?