JavaScript Data Structures: Arrays (pt.1 )

Esther 승연 Kang
2 min readNov 22, 2020

--

This week, I’ll be starting a new series about some of the different JavaScript data structures. This series will dive into the details of each — including what they look like, how we can use them, how we can manipulate them, and examples of each.

This week’s blog will be about Arrays! This is the most basic data structure there is — but there is so much you can do with them. With that being said, the topic of arrays will have two parts to it. The first part will talk about the syntax of and how to use arrays, while the second part will talk about how you can manipulate arrays. Let’s get started!

Super cute shiba (dog) typing away on a macbook while chilling on a couch
Oh you know, just working from home!

What is an Array?

Simple. An array is a basic type of data structure that allows you to store information in memory for later/other usage. Syntactically, all elements in an array are enclosed within a bracket [] and separated by commas.

You can initialize a new array through two ways. 1 — using an array literal, and 2 — using an array constructor.

// array literal initializes with a var, let, or const and variable name let array1 = []
let array2 = ['contents', 'of', 'array', 2]
// array constructor initializes with a 'new' statementlet array3 = new Array(1, 2, 3)
console.log(array3)
//=> [1, 2, 3]

What goes inside the array? Each item is referred as an element. And each element can be… anything, really.

For example, you can store a list of different data types in an array.

let example1 = ['this', 'is', 'an', 'example', 'of', 'an', 'array', 1, 2, 3, '4']

You can even store objects in an array.

let example2 = [
{name: 'Esther', age: 24, location: 'D.C.'},
{name: 'Michelle', age: 24, location: 'Tokyo'}
]

You can access the different elements in an array by calling their index. Each element has it’s own index, starting from 0 and incrementing by one respectively.

console.log(example1[3]) 
//=> 'example'
console.log(example1[8])
//=> 2
console.log(example2[0])
//=> {name: 'Esther', age: 24, location: 'D.C.'}

You can also do a simple modification of the array by accessing the index of the array and setting it equal to whatever you want to change it to.

example2[0] = {name: 'Esther', age: 24, location: 'NYC'} 
// or you can call the location directly instead of writing it all out again
example2[0].location = 'NYC'
// or
example2[0]['location'] = 'NYC'
console.log(example2[0])
//=> {name: 'Esther', age: 24, location: 'NYC'}

And that is the basics of an array! Fairly simple, right? Next time, we’ll talk about how you can manipulate arrays. This is when things get a little confusing, but don’t worry! Soon, you’ll be an array master!

--

--

Esther 승연 Kang

Software Engineer | Flatiron School Alum | Dancer | Looking for Work!