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 Foundations Arrays Methods: Part 1

"Push", "Pop", "Unshift", "Shift" when do you use these in real programming life?

Hello,

I understand what "Push", "Pop", "Unshift" and "Shift" does. But could someone please give me an example when you would use these in real programming life? When do u use these to solve a problem? Can't really see where this would come in handy. :)

Thanks.

1 Answer

Dave McFarland
STAFF
Dave McFarland
Treehouse Teacher

Hi Joel Fredin

Those are all very useful methods. The push() method in particular is probably the most common way of adding an element to an array. It's better than simply using an index value like this myArray[myArray.length]

You can use the shift() method as a way to manage a queue. For example, say you have an array that contains a playlist of songs. To retrieve the next song in the playlist (that is, the first song in the list) you can use the shift() operator:

var nextSong = playlist.shift();

Arrays are a very common and useful way of storing data and there are a lot of methods to work with arrays: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

Thank you Dave! :)

Really good response. Now i've got an insight how to use it in practical purposes. Seriously, this forum is awesome. The responses I get are always fast. I never have to wait more than a few hours. AND, best of all, the answers are always good. Treehouse must be the best educational-website i've tried.

Thanks.