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

What is the purpose of using push and unshift to add items to an array?

Wouldn't it be easier go to the array and add the items opposed to using varname.push[] and varname.unshift[]?

2 Answers

Ari Misha
Ari Misha
19,323 Points

Hiya there! Generally programmers are lazy and we dont like repetitive tasks, and we shouldn't, right? Lets just say you've like 15 entries that you need to add to an array. Are you gonna type everything? Or Lets just say you're getting your data from an external API and its in jSON with thousands of entries and deeply nested. Are you still going to type it out? I hope not! Thats why methods like push() and unshift() exist. In real world problems, you're going to have to deal with data from somewhere else like an External API or Data Scraping files or Databases etc. and dealing with data can be a pain in the ass unless you're really good at Algorithms.

~ Ari

Awesome thank you