Newby Coder header banner

Javascript Arrays


What is an array?

Arrays are objects that contain multiple values stored in a list

Array objects can be stored in variables and can be dealt with like any other type of value, difference being that

Arrays can store one or more data types like strings, numbers, objects, and even other arrays

var arr = ['plant', 795, [0, 1, 2]];

Creating arrays

Arrays consist of square brackets and elements that are separated by commas 

//create empty list
var emptyList = []
// list with initial values
var vowels = ['a', 'e', 'i', 'o']

Accessing and modifying array items

Individual items in array can be accessed using bracket notation

Backet notation consists of list name (or an expression), followed by opening square bracket [, index or position of list item and closing square bracket ]

For example,

vowels[0]

returns a since index of a is 0 (index count starts from 0)

vowels[3] returns o

An item inside an array that is itself inside another array can be accessed by chaining two sets of square brackets together

For example, to access one of the items inside the array that is the third item inside the random array :

var arr = ['plant', 795, [0, 2, 1]];
var nestedElement = arr[2][1];

Value of nestedElement is 2

An item in an array can be modified by assigning a value to the item by placing it in left side of equal = operator and new value on right side

vowels[2] = 'o';

vowels array is then changed to ['a', 'e', 'o', 'o']


Finding length of an array

length property is used to find length of an array

var primeUnder20 = [2, 3, 5, 7, 11, 13, 17, 19];
var len = primeUnder20.length;

Value of len is 8

Looping through an array

for statement is used to loop through items of an array for some condition

For example, to get prime factors of 22

var primeUnder20 = [2, 3, 5, 7, 11, 13, 17, 19];
var factors = [];
for (let i = 0; i < primeUnder20.length; i++) {
  if(22 % primeUnder20[i] === 0)
    console.log(primeUnder20[i]);
}

array methods

Following are some methods which can be accessed by array variables

Converting between strings and arrays

split() method can be used to separate items out from a string into an array of strings

This takes a single parameter, which is the character for which string is to be separated, and returns substrings between separator as items of array

var balls = 'Football,Volleyball,Basketball';

To split at each comma:

let ballArray = balls.split(',');
ballArray;

To convert an array to string, join() method can be used

var n = [1, 2, [3, 4]];
var j = n.join()

Value of j is then 1,2,3,4


Adding and removing array items

let myArray = ['Manchester', 'London', 'Liverpool', 'Birmingham', 'Leeds', 'Carlisle'];

to add or remove an item at the end of an array , push() and pop() can be used

to add or remove an item at the beginning of an array , unshift() and shift() can be used

var arr = [1,2,3]
arr.push('Five');
arr.push(6, 6);
arr.unshift(7);

Resultant value of arr is [7, 1, 2, 3, 'Five', 6, 6]

pop() is used to remove last item of an array

var removed = arr.pop();

It returns the item that is removed

item that was removed is returned when method call completes

Similarly, shift() removes and returns first item

let removedItem = arr.shift();

Resultant value of arr is [1, 2, 3, 'Five', 6]