FAQ: Objects - Pass By Reference

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!

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 Likes

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.

7 Likes

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?

7 Likes

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.

15 Likes

Thank you for the input. It is greatly appreciated!

1 Like

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 Likes


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?

2 Likes

Hey there, I am also learning Js currently. You can change object properties with functions as you can see in the ss.

What you cannot change is the object. I mean like, you can change the spaceships color, but you cannot make it like spaceship = car, you cannot assign a different object to that variable.

2 Likes

Hey, I don’t understand when I type

const remotelyDisable = parameter => {
  parameter.disabled = true;
}

, this was printed up

{ 'Fuel Type': 'avocado oil',
  homePlanet: 'Earth',
  disabled: true }

Why only was ‘disabled : true’ printed up to the console?
I appreciate your help. Thanks!

This is some high quality analogy! Thank you, Sir!

Now, I have another question. It’s a simple one. The ‘hints’ create functions by assigning them to variables using the arrow notation. What would be the difference if I used a standard function declaration? ( function name() {/commands/} ). Is it just a matter of notation/preference?

Is obj a global variable or what. Please I need an explanation

If it is used in this context, then no, it is not global…

let paintIt = obj => {
    obj.color = 'glorious gold';
};
1 Like

Because what your code was, I assume, was: console.log(spaceship);

The original spaceship object didn’t have a disabled property, in that function for any parameter that you have you’ll create a .disabled = true property. So, if you write console.log(spaceship.disabled); You’ll get a return of true.

I’m pretty sure obj in this case is a callback function that will be replaced by anything that is put inside parentheses when you call paintIt().

From my understanding it is for the same differences as usual. The most important thing you have to remember is that using a variable declared function means that it’s not hoisted (not hoisted === can’t use the function before it is declared).

Yeah, I wish it would spend more time explaining the “normal” way to write things before jumping straight to the less readable version when teaching a new concept.

1 Like

I found two links to be particularly useful in understanding the concept of pass-by-reference (and pass-by-value):

specifically read in the order listed here was very helpful:

https://hackernoon.com/grasp-by-value-and-by-reference-in-javascript-7ed75efa1293

1 Like
let greenEnergy = spaceship => {
	spaceship["Fuel Type"] = "avocado oil"
}

let remotelyDisable = spaceship => {
  spaceship.disabled = true
}

spaceship.remotelyDisable();
spaceship.greenEnergy();

console.log(spaceship);

Is this not the correct answer for 8. Pass by Reference? It says to call the functions using the object which I did, and I console.logged it.

let remotelyDisable = spaceship => {
  spaceship.disabled = true
}

There is no parameter needed for this function.

1 Like