FAQ: Objects - Bracket Notation

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

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!

Hey there! I just ran the following snippet

`let propName =  'Active Mission';
console.log(spaceship.propName)

and it returned undefined. How is it possible like the propName and ‘Active Mission’ both point to the same location right?

2 Likes

propName is just a variable, the code you provided it doesn’t like propName a property of an object.

1 Like

Hi Everyone,

I was facing difficulty with understanding what instruction #2 was asking. It may be silly but as a novice, sometimes I tend to overthink the instructions. After trial and error and re-reading the instruction over and over, I came up with the code that gave me the result: “Active Mission true”

Instruction summary:

  1. Its asks to use bracket notation AND the propName variable
  2. log the value of ‘Active Mission’ property

My thought process:

  1. the value of the variable propName is a string: ‘Active Mission’
  2. ‘Active Mission’ is only a property with a value, in the spaceship object
  3. I have to locate both the string value and the property value, and log them together

CODE RESULT:

let propName = ‘Active Mission’
console.log(propName, spaceship[‘Active Mission’]);

I received the green checkmark on this. However, I would like to know if I understood the exercise correctly and if the code result is what was needed.

Thank you!

4 Likes

Hi,
I agree. The instructions for #2 are confusing.
I just wrote:
console.log(spaceship[‘Active Mission’]); //output: true

And then I got a green checkmark for that task.
I then checked the hints, as I thought I was missing out something about the propName variable, but the hint just said: console.log(objectName[variableName])
…which looks like it’s what I did. So… Why is the propName variable even there?

/grumsen

4 Likes

you could have used propName (which is a variable) instead of hard-coding the string when accesing spaceship

1 Like

Ah… I think what confuses me is that the propName is just a variable. It’s not pulling anything from the object and I can’t use bracket notation on the variable alone. If it was pulling the property from the object it would mabye make more sense.

But you’re saying that the correct answer should be:
console.log(spaceship[propName]);

Thanks. Makes sense for me now. :slight_smile:

2 Likes

yes, its indeed just a variable with a string value.

that is indeed the answer the lesson is after

1 Like

Hi,
What I don’t understand is why in this case I shouldn’t surround propName with hyphens, since this seems otherwise to be the default syntax with bracket notation?

hyphens? You mean quotation marks (") or apostrophe (')? Because propName is a variable containing a string

Or more definitively, propName is type string. In JS objects, all keys are strings.

for (key in {1:1, 2:2, 3:3, 4:4}) {
    console.log(typeof key);
}

Output

string
string
string
string
2 Likes

Right, I meant quotation marks (or apostrophe) and I guess I see now, thank you so much!

While on this subject (at least so I suspect) perhaps you could help me out understanding the meaning of literal, as in ‘string literal’? Is it the opposite of a primitive, or is it just another word for primitive? I’ve been through long forum threads trying to figure this out myself, but I’m not sure I get it. If it’s the case that a string literal is a string that is not an object, I guess it should always be enclosed by apostrophes (or quotation marks) whereas a string that points to a variable should not, am I right?

In JavaScript, as with other languages primitive data types are not objects so are given Object wrappers.

"string literal"

variable

Primitive data type - Wikipedia

Variables are actually properties of their scope which if not defined is global, in the context of the window object.

my_string = "string literal"
console.log(window.my_string)
// <- string_literal

All property names in objects are typeof ‘string’. JS handles their difference in the background.

String.prototype.isPrototypeOf("string literal")
// <- false
s = new String("string literal")
String.prototype.isPrototypeOf(s)
// <- true

A literal has no constructor, so has no prototype chain. Above we defined a new instance of the String class. We see that object does have a constructor and is in the prototype chain of String.

"string literal" instanceof String
// <- false
s instanceof String
// <- true

We might conclude that a literal is the equivalent of a primitive, not the opposite. A literal is not data until we assign it. In the process of assignment it is given an object wrapper as determined by its data type.

Chances are all I’ve managed to do is confuse you further. I use the term ‘literal’ to imply the actual thing.

"string" => string literal
42       => integer literal
true     => boolean literal

Must admit, I’m afraid to put my foot in my mouth if I use the term ‘primitive’ to describe the above, but that may well be the case. Literals have primitive data types.

We’ll need to do some more reading to bring about a distinction between the two terms. Going to invite @appylpye to get his take on this question.

5 Likes

Hi, all.

A literal, in programming, refers to the direct representation of a value as opposed to its representation via a variable or its computation via a more complex expression.

This would qualify as a string literal in many programming languages:

"Monty Python"

The following is not a string literal, but in many programming languages it would be an expression that evaluates to a string via the concatenation of three string literals:

"Monty" + " " + "Python"

This could be an Array literal in JavaScript or a list literal in Python:

["Orange", "Cherry", "Peach", "Apricot"]

In JavaScript, we can have object literals, for example:

{
    firstName: 'Eric',
    lastName: "Idle",
    age: 76
}

Here’s a dict (dictionary) literal in Python:

{1: 'one', 2: 'two', 3: 'three'}

In many situations, it is convenient to use literals. We can even pass an object literal as an argument to a function in JavaScript, however, in general, we should instead implement complex types by first defining classes, and then by creating instances of those classes.

8 Likes

In programming, a primitive is generally, but not universally, defined an entity of a built-in type that is not composed of smaller entities. A example, in most programming languages, would be an integer.

A composite is an entity that is composed of smaller entities. JavaScript Arrays or objects would be examples. In Python, and many other languages, a string is primitive. However, in Java, it would be a composite comprised of chars. Opinions differ regarding whether composites that are built-ins should be referred to as primitives.

Concerning literals and primitives, the following would qualify as both:

42

However, consider the following line of Python code:

n = 42

42 is an int literal, and after execution, n refers to that value. However, n is a variable and not a literal because it represents that value indirectly.

7 Likes

Thank you for weighing in, @appylpye! Appreciate your explanation.

3 Likes

Thank you @mtf and @appylpye! My brain started boiling when first reading your explanations, but after going through it multiple times and also diving down other rabbit holes (I can recommend this post https://javascriptrefined.io/the-wrapper-object-400311b29151) I now feel I sort of begin to understand. So grateful for your help!

2 Likes

I’m really happy you chased down that article on wrapper objects. Couldn’t have been more to point. Now we have a question to add to our tool kit, 'Does JS need to coerce this to an object, or not?" to determine if something is a primitive.

As far as literal is concerned, I think we’ve covered that for all intents and purposes. It is the primitive before us that will literally need coercion to become usable data.

Kudos for making a leap in this regard.

1 Like

Not sure if I’m going mad, but how does the variable ‘propName’ end up as part of the spaceship object?

If I’m declaring an entirely new variable, how does its value decide to align with what is inside the object?

2 Likes