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
Marco Asteriti
2,654 PointsDifferent result based on using 'name' as an array name?
I was writing the code for this exercise when it gave me a weird result on the screen. In summary, to populate the student object array, I created four distinct arrays (name, track, achievement and points) each containing 5 values...one for each student. Then I used a for loop to push each of those values as a new object in the 'student' object array.
The weirdness happened when displaying the 'Name' property of each student. Instead of the full name like "Andy", I would get only a letter "A" for the first student...then "n" the next, then "d", etc. But if I changed the name of the array from 'name' to 'name1', it worked correctly! I didn't see name as a reserved keyword...what gives?
I tested this following code on the console, I would get this:
var name = [
"Andy",
"Bryan",
"Charlie",
"David",
"Edward"
];
< undefined
> name
< "Andy,Bryan,Charlie,David,Edward"
> var name1 = [
"Andy",
"Bryan",
"Charlie",
"David",
"Edward"
];
< undefined
> name1
< ["Andy", "Bryan", "Charlie", "David", "Edward"]
1 Answer
Melisa Hamilton
11,729 PointsLooks like 'name' is a property that returns the name of a function. So it is technically a reserved keyword.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name