FAQ: Objects - Accessing Properties

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

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!

How can i access ‘Fuel Type’ property of spaceship object since this property is having space.
below are my tries:
console.log(spaceship.Fuel Type);//SyntaxError
console.log(spaceship.FuelType); //Undefined
console.log(spaceship.‘Fuel Type’); //SyntaxError

9 Likes

you could use associative array notation:

spaceship['Fuel Type']
10 Likes

Yeah, Thank you. :slight_smile:

Hi why are some object property name in ‘abc’ and some without it?
let spaceship = {
‘Fuel Type’: ‘Turbo Fuel’,
‘Active Duty’: true,
homePlanet: ‘Earth’,
numCrew: 5
};

In Js, objects are associative arrays and vice versa. when we have an object, we can access the property using:

object.property

or the associative array notation:

assoc['key']

so we have property and key.

properties have certain naming convention (there can’t be spaces), so then we can use the associative array notation

@stetim94, can you clarify? If the property has a space in its name, we cannot access it using dot notation. Is that correct?

Further question – if so, then is it simpler to use camelCase and not add spaces to our property names? Would that create problems further down the road?

Thanks for your help!

3 Likes

And for hyphens you also need to use key/associative array notation I believe

Maybe we do not always have a choice. The data might come from the user or a existing (large) dataset.

Ahh, yes. By the way, I learned the answer in the next lesson (patience is not my strong suit ), but I will leave this question here for others to read. Thank you!

Of course … learning the applications of code will definitely help with my understanding of this, vs just dabbling in theory!

Thanks so much for your response, @stetim94.

2 Likes

@dev0077889028 @stetim94 I am a little confused reading this… what is meant by ‘abc’ as in

Thank you in advance for clarifying!!

Another thing I was wondering as I got to this lesson is, in a key-value pair, can we use the value to access the key? And if so, why would we want/won’t want to do that?

Thanks so much! :smiley:

certain keys are written as string ('Fuel Type' for example). The question was why.

not very common, but not impossible either. Not sure there is a built-in function, you can always write your own. The question is: What to do with duplicate values? Keys are unique, values aren’t

1 Like

Ah ok, thank you for the clarification!

By “write your own”, do you mean that I can just create one? How would I go about doing that (sounds very interesting!!)?

That makes sense- so then, would merging keys with the same property name be possible? Eg.

let house = {
     color: "white",
     color: "teal"
}

and instead, write color: ["white", "teal"] -or something of the sort?

Thank you very much for your time and help! :+1:

you could write a function which implements a loop, and finds all the matching value, or return the key of first match found. Several possibilities.

then you have a single key, so that is possible. The value is then a array, or could even be another dictionary.

I’d like to try!! Is there a solution code I can compare mine to? :smiley:

Ah ok, thank you for confirming! So just to clarify, if we have different keys with the same values (“because values aren’t unique”), that is okay?

Thanks so much again!

nope, but I can have a look at your code. Is a good challenge, first specify what your function should do. Then write the function and test the function with your different cases/scenarios

this is where the real fun in programming begins.

that is perfectly fine, the question is: How are you going to handle that in the function you are going to write?

@stetim94 thank you so much!! I think I will still need to do a little learning before I can write the function (and make it work), but it’s something I really want to try out -and like you wrote, fun indeed!

Hmm…I’m thinking maybe I’ll start off simple, so that each key and value pair names are unique- and then maybe do a little bit of tweaking …hopefully I’ll get it :sweat_smile:

Thank you so much for your inspiration!

1 Like

There’s a difference between a property and a key?

In JavaScript an object is an associative array and vice versa.

so when using dot or property notation:

obj.prop

I usually use the term property. When using the associate array notation:

obj['key']

I would say key.

Not sure if that is one 100% accurate, and I think I use property more.

1 Like

By that, do you mean we can use bracket notation to access an object’s properties just like we can use bracket notation to access an array’s elements?