JavaScript: Data Structures (Part 1 - Arrays)
Tags: javascript, datastructures, coding, webdev
Published: 2018-11-26 21:57:42

---
title: JavaScript: Data Structures (Part 1 - Arrays)
published: true
description: 
tags: #javascript #data-structures #coding #webdev
---
![alt text](https://thepracticaldev.s3.amazonaws.com/i/5gwwcx6krdmlzmmpp39r.jpg)

<h3>Data Structures: Arrays, Sets, and Maps.</h3>


Data structures are used to store lists of values, these data structures are called arrays, sets, and maps. But what is data structure? According Wikipedia, “In computer science, a data structure is a particular way of organizing data in a computer, so it can be used efficiently”,  to which I would add that using logical statements allows you to control the flow of a program, as well as loops that would allow us to repeat blocks of code over and over again as needed in our code.

<h3>The Array Data Structure</h3>

An array data structure or an array is an ordered list of values, or a collection of elements (values or variables) identified by an index or key. The most simple type of array data structure is a linear array.

Creating an array literal will require you to write a pair of square brackets.


***Example:***

```
const myArray = [ ];
<< [ ]
```

We can add a constructor function

***Example:***

```
const myArray = new Array( );
<< [ ]
```

Arrays are not primitive values but a special built-in object, like when using the “typeof” operator:

```
typeof [ ];
<< ‘object’
```

<h3>Initializing an array</h3>

But, How do we initialize an array? Well, we can create an empty array literal.

***Example:***

```
const heroes = [ ];
```

Now, we can find the value of element 0 (zero) in our heroes array

```
heroes [0];  // to access a specific value in an array, we write its index inside the square brackets
<< undefined
```

<h3> Adding values to an array </h3>

Place a new string by assigning the element 0 (zero).

***Example:***

```
heroes[0] = ‘Superman’;
```

We can treat items in the array as a variable and changing its value by using the assignment operator =

***Example:***

```
heroes[0] = ‘Batman’;
```

We can add more values using different indices:

```
heroes[1] = ‘Wonder Woman’;
heroes[2] = ‘Flash’;
heroes[5] = ‘Aquaman’;
```
You can repeat the operation until you add all the elements you want or need in your array.

Then, you can review your array by typing your array name (heroes) on your console and look at it.

```
heroes;
<< [‘Batman’, ‘Wonder Woman’, ‘Flash’, undefined, undefined, ‘Aquaman’]
```

You can see that those elements assigned with an index appear in the list by its name, but any other unused slots appear as undefined.

<h3> Creating array literals </h3>
We can create array literals to avoid adding values one by one.

***Example:***
```
const avengers = [‘Captain America’, ‘Iron Man’, ‘Thor’, ‘Hulk’];
<< [‘Captain America’, ‘Iron Man’, ‘Thor’, ‘Hulk’]
```

<h3>Removing values from arrays </h3>
Using the ‘delete’ operator allows us to delete an item from an array.

***Example:***

```
delete avengers[3];
<< true
```

The action will delete the value store in the index 3 (‘Hulk’), but it also leaves the index as undefined because even when deleting a value the space is still there, meaning that the array still has the same number of elements but the one we deleted will be undefined.

```
avengers;
<< [‘Captain America’, ‘Iron Man’, ‘Thor’,  ‘undefined’]
```

<h3> Destructuring arrays <h3>

Destructuring refers to take values out of the arrays and presenting as individual values, this allows us to assign multiple values at the same time.

```
const [x, y] = [1, 2];
```
It means that each variable exists outside the array and you can check its value individually.

***Example:***

```
x
<< 1

y
<< 2
```
Or,

```
[x, y] = [y, x];
x
<< 2

y
<< 1
```


This is [Arrays](https://dev.to/banesag/javascript-data-structures-part-1-4eb5) (Part 1) of a series of 3 posts about data structures, [Sets](https://dev.to/banesag/javascript-data-structures-part-2-40cc ) (Part 2) and [Maps](https://dev.to/banesag/javascript-data-structures-part-3---maps-149i) (Part 3) will complete the series.