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

Eli Levit
Eli Levit
4,482 Points

not clear

Why do we write: const text = cards[id][side]; and not : const text = cards[id].side; //????

1 Answer

Dot-notation is most commonly used in JS to access properties of objects, which means that the property name is given after a full stop character. For example: Given:

<html>
<form id="myForm" action="/">
  <div><input name="foo"></div>
</form>
</html>

We can say:

<script>
var myForm = document.getElementById("myForm");
var input = myForm.foo;
</script>

Here we call the getElementById property of the document object as a function, and then we take the foo property of the form (which is the input with the name "foo"). Square bracket notation lets us do exactly the same thing, but with different syntax:

<script>
var myForm = document["getElementById"]("myForm");
var input = myForm["foo"];
</script>

You can also read more here : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors

Hopefully this helped.