My Javascript Journey

Data Structures

Allan Sendagi
3 min readMar 28, 2019

Part 6

source: coderforevers.com

So lets look at our first data structure.

What is a data structure?

So far we have looked at numbers, strings, booleans, undefined and functions. The question you might have is; how do you store all of them?

Keep in mind that a variable is like a desk drawer, but you don’t want to throw everything in there and have a huge mess — or you don’t want to throw just a marker and close that drawer. Ideally you want to store more things but organised.

Data structures are like compartment holders in your desk. Different data structures are good at different things. They give us the ability to store things — say in alphabetical order, and access it in a way that is useful to us.

Arrays

So an array as we can see is still a variable, and we can see strings as well. The only thing that’s different are the square brackets. Therefore an array is a way to organise information.

But how do we grab one of these 4 items from our desk drawer?

So, variable namesquare brackets a number and — square brackets. In computer science developers like counting from 0. So 0 is the first number, then 1, then 2, then 3…. we always start counting from 0.

If you want to get Apples from this list, you do 1.

Now what kind of things can arrays hold or is it just strings?
We can do variable numbers — 1,2,3,4..; we can have booleans, and functions.

We can also make arrays within an array.

You see, what I just did here? We have an array, that has an array of [Banana, Apples, Oranges, Blueberries].

Arrays are nice because JavaScript has predefined methods for them. For now think of them as functions we can use on arrays.

Here are some of them:

array.shift

The shift() method removes the first array element and "shifts" all other elements to a lower index

array.pop:

The pop() method removes the last element from an array:

array.push:

The push() method adds a new element to an array (at the end):

array.concat

The concat() method creates a new array by merging (concatenating) existing arrays:

For further reading on arrays:

See you in part 7.

--

--

Allan Sendagi

Technology will save Homo Sapiens from extinction. I document my journey learning these technologies https://www.linkedin.com/in/allansendagi/