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

Julie Myers
Julie Myers
7,627 Points

Use of Arrays in the Real World

In the real world, how often are Arrays used compared to a backend database such as mySQL or MongoDB? It seems that arrays are important to know, but when would I use an array over an back end database?

6 Answers

Dave McFarland
STAFF
Dave McFarland
Treehouse Teacher

Hi Julie Myers

It's not an either/or choice. You use a database to save information -- for example, to save your user profile here at Treehouse, or to send data via a web form as in buying a product from Amazon. You'll do that using server-side programming using a language like PHP, Python, Ruby, Java, or JavaScript (when using Node.js).

You use arrays in your programming -- it's not uncommon to retrieve data from a web form as an array (using whatever server-side programming language you want) then put it into a database,

You can also retrieve data from a database as an array and use JavaScript in the web browser to process that information. For example, in my AJAX Basics course I discuss how Flickr sends information about photos as an ARRAY of JavaScript objects. You can go through that array to get a list of photos and display them on a page.

So basically, you use databases to store data for a long period of time, and an array to process data temporarily.

Dave McFarland
STAFF
Dave McFarland
Treehouse Teacher

You use arrays all of the time in programming. Whenever you need to keep track of an ordered list of items, you'll use an array: a list of songs, a list of each keystroke a user clicks. Even in the JSON data format, you'll often use an array to hold a list of objects.

Databases provide a different functionality: they let you keep data over time. You can retrieve that data and update it and view it from other computers using other web browsers.

You will often retrieve information from a database and store it for local processing in an array.

Justin Black
Justin Black
24,793 Points

In the general sense yes, but the actual use of arrays in javascript is slim to none... It all really depends on the use case of course, but in the general context, an object is the better option for most cases... Though, arrays are very handy, such as in my graphing scripts I use arrays to define my data points in my graphs.

Dave McFarland
Dave McFarland
Treehouse Teacher

Hi Justin Black

Maybe we're talking about different things: server-side transfer and storage of data, and client-side creation and manipulation of data.

Arrays are a HUGE part of JavaScript programming -- in fact most programming languages have arrays (or an equivalent data structure). Just a quick look at the Mozilla Developer Network's page on the Array object -- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array -- shows how many array methods are available in the language.

Justin Black
Justin Black
24,793 Points

For most things, you are going to use the backend database to return a JSON Object to your JavaScript... But arrays are very powerful, and useful.. I rarely use these, except for static configuration options of a jQuery plugin that I write - but even with these, objects would do a better job....

I use arrays a lot early on in my development to test functions that will access information such as from objects or JSON tables. Arrays are very quick and easy to write out, as opposed to using an Ajax call or writing out an entire object before making sure the functions you will use to access your information are working properly.

Julie Myers
Julie Myers
7,627 Points

HI Justin, Thanks. :)

Julie

Julie Myers
Julie Myers
7,627 Points

To Dave and Justin,

I am wondering about both server-side transfer and storage of data, and client-side creation and manipulation of data. Is it more of a programmers preference to use a database over a JavaScript array? Or are there really solid reasons why JavaScript arrays are just not used in the real world much, if that is how it is? In what cases would I used a database, and in what cases would I use an array?

Thanks!