Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.