My Javascript Journey
Data Structures
Part 6
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 name — square 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
array.pop:
array.push:
array.concat
For further reading on arrays: