JavaScript Arrays

JavaScript Arrays #

Can store any data type.

Order matters.

Can be of mixed data types.

let someArray = ['thing1', 'thing2', 'thing3'];

Accessing #

Index starts at 0.

someArray[0] // returns `thing1`

Update #

someArray[2] = 'a new value';

Elements in arrays defined with const are still mutable.

Nesting #

Arrays can store other arrays.

const someArray = [[1], [1, 2]];

Elements can be pulled from within nested arrays with brackets.

someArray[1][0]; // returns 1

Properties #

.length #

.length gets the array length

console.log(someArray.length)

.pop #

remove last element. does not take any arguments. Also shows value of last element.

someArray.pop()

.push #

adds an element to the array end

someArray.push('another1', 'another2')