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 trialAaron Coursolle
18,014 PointsI did some experiments...?
And I got the same result as if I had used a "push". Is that really what is happening?
var numbers = [ 1, 2, 3, 4, 5, 6 ];
numbers[numbers.length ] = 7;
alert (numbers);
//output: 1, 2, 3, 4, 5, 6, 7
var evenNumbers = [2, 4, 6, 8, 10, 12 ];
evenNumbers[evenNumbers.length] = 14;
alert (evenNumbers);
//output: 2, 4, 6, 8, 10, 12, 14
var randomNumbers = [34, 21, 49, 2, 8, 208 ];
randomNumbers[randomNumbers.length] = 50;
alert (randomNumbers);
//output: 34, 21, 49, 2, 8, 208, 50
var boolValues = [true, false, false, true, true];
boolValues[boolValues.length] = false;
alert (boolValues);
//output: true, false, false, true, true, false
var mixedValues = ["banana", "green", 45, false]
mixedValues[mixedValues.length] = "Hires";
alert (mixedValues);
//output: "banana", "green", 45, false, "Hires"
1 Answer
sean murphy
9,435 PointsYes! It is nearly identical to push. When you use array.length as a location of the array, you are accessing a new entry in the array. This is because arrays start their index at 0, and then increase. So array[0] = "some value" and array[1] = "another value" would have array.length == 2. Since you are now setting the .length location to a new value, it is functionally the same as the push() method.