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 Multiple Items with Arrays Create a Two Dimensional Array

J P
J P
1,620 Points

Hi, I'm having trouble with 2-dimensional arrays. I get a There was an error with your code: TypeError: 'undefined''.

Hope you can help!

script.js
var coordinates = [ [1, 2] [0, 1] [1, 2] [0, 1] ];
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript Objects</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>

3 Answers

Paras Bokhari
Paras Bokhari
11,522 Points

Hi Joseph,

You must add commas in order to separate each item within an array. That's why it's very common to enter each item in a two-dimensional array on a separate line.

const coordinates = [ 
    [1, 2],
    [0, 1],
    [1, 2],
    [0, 1] 
];

console.log(coordinates) // logs the array.

Please note: the last item within an array will not have an ending comma.

Hope this helps.

Your arrays should be separated by commas

J P
J P
1,620 Points

Thanks so much!