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]
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
// 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
- push()
- shift()
- unshift()
- length()
- sort()
- reverse()
You can learn about other array methods at:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
Let's take a look at some Code!