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 Loops, Arrays and Objects Tracking Data Using Objects Create an Array of Objects

victor E
victor E
19,145 Points

i was certain this was correct

not sure why my syntax is wrong.

script.js
var objects = [
   var object_a = {object1: thing1, object2: things2}, 
   var object_b = {object3: things3, object4: things4}, 
   var object_c = {object3: thing5, object2: things5}
]
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript Objects</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>

2 Answers

Dane Parchment
MOD
Dane Parchment
Treehouse Moderator 11,077 Points

It should look like this:

var object_a = {object1: thing1, object2: things2};
var object_b = {object3: things3, object4: things4};
var object_c = {object3: thing5, object2: things5};

var objects = [
   object_a,
   object_b,
   object_c
];

The problem is the following syntax:

var object_a = {object1: thing1, object2: things2};

Is what we call a variable declaration, you can't use a variable declaration as a (non-parameter) value. That is what is the syntax error you are running into. Instead, you must pass in the value of the declaration.

victor E
victor E
19,145 Points

that makes a lot sense thank you!

victor E
victor E
19,145 Points

I tried it and it didn't work :(

Dane Parchment
Dane Parchment
Treehouse Moderator 11,077 Points

probably because the variables thing1, thing2, thing3, thing4, thing5 don't exist. I was just correcting your syntax, I assumed that you had already created and assigned values to those variables. Otherwise, it should look like this:

var thing1 = 1, things2 = 2, things3 = 3, things4 = 4, things5 = 5;
var object_a = {object1: thing1, object2: things2};
var object_b = {object3: things3, object4: things4};
var object_c = {object3: thing5, object2: things5};

var objects = [
   object_a,
   object_b,
   object_c
];

or more concisely like so:

var objects = [
   {object1: 1, object2: 2},
   {object3: 3, object4: 4},
   {object3: 5, object2: 5}
];
victor E
victor E
19,145 Points

yeah they wanted me to just put the objects inside the array without defining them outside of it. the second example worked. thank you!