Arrays , what are for?

I just some stuff about arrays, how can they be useful in the future? Accessing entries in an array would be useful for knowing that it is multi-dimentional and can’t really know what indexing may give or retrieve ?

Arrays are just 1 data structure among many. JavaScript’s Array implementation is rather flexible compared to other languages. As it provide methods that can make an array act like multiple other types of data structures, like a Stack or Queue.

So if Arrays are just an implementation of a “data structure”, then what are data structures for?

Data structures are defined as:

a data structure is a data organization, management, and storage format that enables efficient access and modification.

So Arrays specifically are useful for keeping track of multiple things in a specific order.

Using this forum as an example, each thread is made up as an array of posts, so the data structure would look similar to:

const posts = [originalPost, firstReply, secondReply];

const firstPost = posts[0]; // remember Arrays start at 0!
const numberOfPosts = posts.length:
const notFirstPosts = posts.slice(1); // get the 1st index to the end, will return only 2 posts

function addPost(post) {
  posts.push(post); // add a post at the end of the array

Odds are you your program will need to deal with dynamic sets of data, these sets could come in the form of an array. There are other data structures out there besides just Arrays, but Arrays are one of the more common ones you will run into.

Other data structures might be more useful in other situations, and work with algorithms to solve common problems. For example, an array might be good for a list of data, but there are other better data structures for other situations. For example, in the above example with the array of posts, if I gave you a unique id of a post to find, an Array would require you to iterate through the list until you found that item. Where-as using a data structure like a hash-map would allow you to just get that post instantly. The difference might not be noticeable for small amounts of posts, but it can be dramatically different if there are say thousands, or millions, or even billions of posts!


This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.