Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

JavaScript Objects in this book are hard to understand.

I am reading book called 'eloquentjavascript' and in it is a paragraph that confuses me a lot and I cannot make sense of it. It says

"The two typical ways to access properties in JavaScript are with a dot and with square brackets. Both value.x and value[x] access a property on value—but not necessarily the same property. The difference is in how x is interpreted. When using a dot, the word after the dot is the literal name of the property. When using square brackets, the expression between the brackets is evaluated to get the property name. Whereas value.x fetches the property of value named “x”, value[x] tries to evaluate the expression x and uses the result as the property name."

source

I cannot understand, I am trying my best but this chapter seems hard

1 Answer

It is trying to explain the difference between using dot notation to access the property of an object and bracket notation. A simpler way of explaining it might be to look at a code example.

Let's say you had code that looked like this:

var x = "y";

exampleObject = {
  x: "Hello World",
  y: "Andre"
}

console.log("Dot notation example: " + exampleObject.x);
console.log("Bracket notation example: " + exampleObject[x]);

What do you think the first console statement will print out? When you use a dot to access an object JavaScript will look for a property whose name matches exactly with the text you typed after the dot. So in this case it will look for a property called x which means that the printed output will look like this:

Dot notation example: Hello World

What do you think the second console statement will print out? When you use bracket notation with an object JavaScript does not treat the text you type as the literal name of the property, it instead treats it like regular code. Meaning that in the above example it will actually first look for a variable called x then it will take the contents of that variable and use it as the name of the property you want to access. Since there is a variable called x which stores the string "y" the printed output will actually look like this:

Bracket notation example: Andre

The disadvantage of dot notation is that you need to know the exact property name you want to access while typing your code. In some cases that's not really possible like if you are dealing with objects you get from a remote server, and in other cases you might have a need to have the property that is accessed differ based on some previous code. Both of those scenarios are easily solved when using bracket notation since you can figure out the name while the program runs and just assign it to a variable, which is then used to access the property with bracket notation.

You can also type strings directly when using bracket notation of course, like this:

exampleObject["x"];

In that case you access whatever property the text of the string matches, with the above code the x property would be pulled out. Doing that is not that common though since there's not much upsides to it compared to using dot notation. The only real point is if you have a property whose name contains a space. Having a space in a property name is technically valid (though uncommon) but spaces are not valid when typing names using dot notation, so if you are dealing with a name like that then you have to use bracket notation to access it.

That's a great explanation, the first block of code just about says it all :)