[Arrays]

Learning Objectives

  • create an array
  • choose an element
  • change or replace an element 
  • add and remove elements
  • find the size of an array
  • loop through an array

Definition

An array is a collection of items in an ordered list

Arrays in JavaScript are represented using square brackets

 

[ ]

   ['one', 2, true]

Each item in an array is called an element and is specified using an index

index value
0 'one'
1 2
2 true

Indexes begin with a 0

let arr = ['one', 2, true]

Dynamic

Unlike other lower level languages, arrays in JavaScript are dynamic.

 

This means we can add to and remove from arrays as we choose.

let arr = ['one', 2, true]

Arrays can also contain a mixture of data types

There are several ways to create a new array

// creates a new instance of an array
let arr1 = new Array(); 

//creates a new array with four elements
let arr2 = new Array(4); 
 
//creates an array with four elements and their values
let arr3 = new Array(2,'three', 'four', 5); 

//most common ways to create an array
let arr4 = []; 

let arr5 = [2, 'three', 'four', 5]; 
# PRESENTING CODE

Array Creation

There are many methods we can use on arrays including:

- push()

- shift()

- unshift()

- length()

- sort()

- reverse()

{code}

Let's take a look at some Code!

Code

By JD Richards