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 Loop Through Objects Store Objects in Arrays

Austin Schneider
Austin Schneider
4,919 Points

Creating the Objects

I feel like this skipped over some information. In the previous videos we were creating objects by declaring them like variables:

''' const students = { name: 'Jesse', grades: [80, 85, 90, 95] } '''

But now in this video Guil just changes the nested arrays into objects with basically a simple switch of the brackets to curly.. but no mention of the inner workings behind this or that this is how the object is created.

Are the curly braces what creates the object?

2 Answers

Hi Austin, There are different ways to create new objects:

  1. Define and create a single object, using an object literal.
  2. Define and create a single object, with the keyword new.
  3. Define an object constructor, and then create objects of the constructed type.

Object literal is the easiest way to create a JavaScript Object.

Using an object literal, you both define and create an object in one statement.

And yes an object literal is a list of name:value pairs (like age:50) inside curly braces {}.

Happy coding ☺️

Hi Austin,

in this video, he creates an array of objects instead of an array of arrays (multidimensional array)
An array of objects is easier to read and understand because in objects you have the properties whichs says what it is, and a value.
So in the video you know what exactly is the question, and what is the answer and you can access the values by the property name.
All these objects are stored in an array because you can loop through this objects easily with e.g. a "for-loop".
With an array of arrays, you can only access these values by the index number and it is not clear, what stands for what and since these are arrays in an array, you have to use to index numbers.

E.g.:
array of arrays:
questions[0][0] = "How many planets are in the Solar System?"
questions[2][1] = 6

array of objects
questions[0].question = "How many planets are in the Solar System?"
questions[0].answer = 8
questions[1].question = "How many continents are there?"
questions[1].answer = 7
etc...

So it's a way more easier to understand and read the code and work with it.

Hope this answer helps :)