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

Abe Layee
8,378 PointsObject is returning undefined
I am trying to print out the first student name to the console.log() , but I keep getting undefiend instead of the properties and value within the object. What am I am doing wrong here
var getStudent = document.getElementById('student');
function printStudent() {
var students = [
{
Name:'Abraham Swaray',
Track: 'Front End Developer',
points: 3323,
},
{
Name:'Mohammed Kanneh',
Track:'Back End Developer',
points:10000,
},
{
Name:'David Beckham',
Track:'Web Design',
points:1200,
},
{
Name:'Layee Abe',
Track:'IOS Developer',
points:13000,
},
{
Name:'Joseph Max',
Track:'Android Developer',
points:12400,
}
]
for(var i in students) {
console.log(i,':',students[i][0]);//shouldn't this return the first student to the console log?
}
}
printStudent();
3 Answers

Jacob Bender
15,300 PointsIf you just want that first name, use
console.log(students[0].Name); <- here you are asking it to log the first student objects name property in the student array using dot notation.
If you want to print say, just all the names, you would use a for loop as follows:
for (var i in students) {
console.log(students[i].Name); //Loops through every object, displays Name property for each object found
}
Hope this helps.
Edit: as a note, students[i][0] is asking for an array inside an array. If i is 0, you are asking for [0][0] in a 2d array. See http://www.techrepublic.com/article/multidimensional-arrays-in-javascript/6310694/

Jason Anders
Treehouse Moderator 145,862 PointsHi Abraham,
I noticed a few things that would be causing you grief with this.
- I changed the key name in your objects to lower case.
- You have an unwanted (and un-needed) comma after the last key/value pair in each object in the array (after "points").
- In the console.log, you want to concatenate the string, so you need to use + and not a comma.
- You were calling the part of the Array like it was an object. Object use name[number], and Arrays need either dot notation (arrayName.key) or square brackets, but with the key name Array["key"].
All fixed, your code will look like this:
var getStudent = document.getElementById('student');
function printStudent() {
var students = [
{
name: 'Abraham Swaray',
track: 'Front End Developer',
points: 3323
},
{
name:'Mohammed Kanneh',
track:'Back End Developer',
points:10000
},
{
name:'David Beckham',
track:'Web Design',
points:1200
},
{
name:'Layee Abe',
track:'IOS Developer',
points:13000
},
{
name:'Joseph Max',
track:'Android Developer',
points:12400
}
]
for(var i in students) {
console.log(i + ':' + students[i].name);//shouldn't this return the first student to the console log?
}
}
printStudent();
And it will print the first and last name, because that is what have in the "name" key.
Hope that helps to clear it up for you Jason :)

Abe Layee
8,378 PointsThank you for clearing things up.