FAQ: Objects - Creating Object Literals

This community-built FAQ covers the “Creating Object Literals” 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 Creating Object Literals

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!

Which special character is in the key ‘Fuel Type’?

2 Likes

" ", It’s just a space.

First task: Do I really need quotation marks around the value “silver”? Had to put them there to get the answer accepted.

Hello, @byte5091529437.

Yes. You do need quotation marks around literal string values.

1 Like

Why do we not need quotation marks around color as well?

2 Likes

Hello, @melissa0021.

Welcome to the forum.

To put your question in context, the code you’re referring to is below:

let fasterShip = {
  'Fuel Type': 'Turbo Fuel',
  color: 'silver'
};

In this snippet of code, faserShip is an object. It has 2 properties: 'Fuel Type' and color. Property names are similar to variable names. Similar, but not exactly like variable names. We can’t name a variable Fuel Type with or without quotes. In JavaScript the convention for a variable would be to name it fuelType. If we called our property fuelType it would not require quotes. Property names, unlike variable names, can have spaces in them as long as we wrap the property name in quotes: 'Fuel Type'. The short answer to your specific question is because the property name: color doesn’t contain any spaces quotes are not required. The values ('Turbo Fuel' & 'silver') of the properties require quotes because they are string literals.

8 Likes

Why not use camelCase instead of adding a space forcing the use of quotations

3 Likes

Perhaps camelCase was not used in this example to showcase that a space is a special character and needs quotation marks. The example also shows you can have a space in a property name and that is how it is different from a variable name which doesn’t allow a space.

2 Likes

I think it does a better job confusing new coders

4 Likes

In the second slide of the Objects Lesson: Creating Object Literals.

Are Objects often declared with a “let” declaration?
Is there a reason for it?

Or is it just that it doesn’t matter in this case?

I feel like this should be touched on, when introducing a new topic.

I thank in advance for your future answers :slight_smile:

can we use fuelType instead of ‘Fuel Type’?

You could, yes.

In the topic Classes , under Inheritance 3 , task 5 i am getting an error for passing values for property certificates, could you please help me?

A general rule of thumb is to always declare your variables with the const keyword unless you’re certain that you’ll reassign it a value later on in your code

Something important to note is that even if you declare your variable with const, the object can be mutated meaning we can add properties to the object, delete properties from the object, and modify existing properties. The only thing constant is the bind between the variable and its value. So you can’t reassign a value to a constant variable

const obj = {
  foo: 2,
  bar: () =>  {
    console.log("Hello, World!");
  }
}

obj.foo = 5;

console.log(obj.foo); // 5

obj.baz = "baz";

console.log(obj.baz); // "baz"

obj = {}; // TypeError: Assignment to constant variable

Even though I understand the syntax of objects, I don’t know what they actually are. Can someone please explain what objects are?

What I understood so far is that objects are used to group related data and functionality. Therefore, we can use objects to create models of real-life things - like a basketball. An object is made up of key-value pairs. Keys (also referred to as properties) are basically the different characteristics of the objects (the color, the size, etc.) and values are simply the values we assign to the property (“red”, “1cm”, etc.)

Is that it?

let fasterShip = {
  'Fuel Type': 'Turbo Fuel',
  color: 'silver'
};

console.log(fasterShip);
console.log(`With string interpolation: ${fasterShip}`);

Result:

{ ‘Fuel Type’: ‘Turbo Fuel’, color: ‘silver’ }
With string interpolation: [object Object]

Why doesn’t string interpolation work with objects?

let spaceship = {
‘Fuel Type’: ‘diesel’,
color: ‘silver’
};
Do objects need its variable’s second word’s letter to be capitalized?
(i.e. can it be like this:
let spaceShip = {
‘Fuel Type’: ‘diesel’,
color: ‘silver’
}; )

In the introduction lesson 7 types are listed, but array is not in the list. How come? Isn’t array a data type? Or is it a special kind of an object? Please elaborate.

Why does ‘silver’ need quotations? It doesn’t have any special characters. Also, is there a reason ‘Fuel Type’ is capitalized?