FAQ: Objects - Property Assignment

This community-built FAQ covers the “Property Assignment” 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 Property Assignment

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!

When deleting a property that has 2 words, e.g. ‘Secret Mission’, from an object, how can you use the dot operator? For example:

delete spaceship.‘Secret Mission’; // this returns an error for me

I can only use the bracket notation in this case?
delete spaceship[‘Secret Mission’]; // this works fine

8 Likes

That’s not a valid identifier so, no, you can’t use dot notation for that

4 Likes

We can create object with two ways :

spaceship.numEngines = 3;
spaceship[“numEngines2”] = 4;

When i tried create .invert() object with two ways :

invert (object) {
let invertedObject = {};
for (let key in object) {
invertedObject[object[key]] = key
};
return invertedObject;
}
This works fine but :

invert (object) {
let invertedObject = {};
for (let key in object) {
invertedObject.object[key] = key
};
return invertedObject;
}

This doesn’t work , Why ?:roll_eyes:

2 Likes

delete objectName[‘Property Name’];
delete objectName.propName;
this is the example they give to delete. Does it not work because it has space in the key? "Secret Mission??? I to am trying to figure it out?

thanks

space isn’t representable in dot notation but isn’t otherwise special

> o = {'abc def': 5}
{ 'abc def': 5 }
> delete o['abc def']
true
> o
{}
/* 
OBJECT: lambo
KEY: garageLocation, "door type", color, "still available"
VALUE: "bay area", "suicide door", "fancy shmancy", "yes"
if the KEY has "two words" or "special symbol", they must be included in quotation marks
*/

let lambo = {
  garageLocation: "bay area",
  "door type": "suicide door",
  color: "fancy shmancy",
  "still available": "yes"
}
console.log(lambo);

//ACCESS TO KEYS
console.log("one word KEY color (dot): "+lambo.color);
console.log("two words KEY door type [square brackets]:"+lambo["door type"]);

//KEY CHANGE
lambo["door type"] = "regular"; //assigning new property
console.log("KEY'door type' changed ")
console.log(lambo); 

//Deleting property from an object with delete operator
delete lambo.color;
delete lambo["still available"];

//Priting updated object lambo, without two last keys
console.log(lambo); 


Has anyone encountered a problem going pass Step 1? I couldn’t move on to Step 2 even after my code was correct. Maybe I’m not seeing it correctly but is the code below for step 1 correct?

spaceship.color = ‘glorious gold’;

When I try a delete operate with a string property name, do I have to use the bracket notation [ ]? When I tried to run the string using delete spaceship.‘Secret Mission’; I get syntax error.
However, when I utilize delete spaceship[‘Secret Mission’]; there is no error ( and this is also the correct solution).

having an issue with “secret mission” what’s the correct way of accessing 2 seperate word as propName

delete spaceship[‘Secret Mission’]; This will help guys…

Why some property names has ‘string’ like = 'Fuel Type’ and some doesn’t has like = homePlanet and mission

const spaceship = {
‘Fuel Type’: ‘Turbo Fuel’,
homePlanet: ‘Earth’,
mission: ‘Explore the universe’
};

Thank you!

Property names that contain white space must be in quotes.

1 Like

What is the difference between reassigning and mutating?

They’re fairly synonymous terms.

a = 1

a = a + 1

Above we reassigned a to reference a new value.

a = [2, 4, 6, 8, 10]

a[2] *= 2

Above we mutated an element of the array.

2 Likes

I get what happened in the reassignment but could you explain what happened in your example about the mutation

Log the array and check the third element.

1 Like

oh I get it! So does that mean that there will be another copy of array a that will have the value of a = [2, 4, 12, 8, 10]?

Thank you for always answering my questions, it really gives me a more in depth understanding of the topic :smiley:

1 Like

Not a copy, the same array, only mutated. a still points to the same object.

That is the basic difference between the two terms mentioned. In the first example, by re-assigning a a new value, a now points to a different object than before. That’s when the object was a number. When we made a into an array changing the array values did not change what a references. Making sense?

1 Like

Yeah it makes sense now

1 Like