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 Adding Data to Arrays

Steven Good
Steven Good
2,303 Points

Do I really need to use these 3 methods when I can simply add data in the original []?

The two different ways below, produce the same kind of results. The first methods seems easier to me because there is less code, and it's clear to view the strings in list order. I guess it depends on a person's preference, but to me, I can't see the benefit of using the push and unshift methods. Could anyone advise? Thanks

var playList = [ 'David Silva', 'Yaya Toure', 'Pablo Zabelta', 'Sergio Augero', 'Shaun Wright Phillips', 'David White' ]; printList(playList);

var playList = []; playList.push('David White); playList.push('Shaun Wright Phillips', 'Sergio Augero''); playList.unshift('David Silva'); playList.unshift('Yaya Toure', 'Pablo Zabaleta'); printList(playList);

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! The difference is essentially this: in the first example you're hard coding your values. Let's say for instance you have a website where a user can add tutorial videos to a playList in a queue. That user can then remove a playlist or add a playlist. They can also add a video to a playlist or remove it.

What we're looking for is a way to add things to the playlist or remove it programmatically. Maybe you have thousands of video tutorials. You have no way to know which ones a user might want to view. You need them to choose for themselves. This will make your website much more flexible and user-friendly. Hope this helps! :sparkles:

Steven Good
Steven Good
2,303 Points

Thanks Jennifer for clarifying this. Really appreciate this! The community here is so supportive and helpful!