JavaScript

Array

We have worked with variables, but now is it time for arrays.
Arrays are list-like objects. It consists og array elements which are made up of an array name and can store any type of data.

To create an array must we use brackets []. This array has nothing inside it

let fruits = [];
console.log(fruits);

Arrays are zero-indexed which means they start with the index number 0.

[true, false, true];
[0] [1] [2]

Array syntax

To create an array with data inside it, you put the value inside the brackets and separate it by commas.

let fruits = ['Apple', 'Banana'];
console.log(fruits);

When you are making arrays does linebreaks not matter.

let fruits = [
'Apple',
'Banana'
];
console.log(fruits);

Do not place any comma after the last value.

Array datatype

Inside an array can you put different type of data, including strings, numbers, boolean values and objects.

let myArray = [5, "Hi there", true];
let myArrayString = ["Hello", "Hi", "Hej"];
let myArrayNumber = [5, 6, 10, 100, 1000];
let myArrayBoolean = [true, false, false, true, true];

It is not a good idea to store diffrent data inside them.

let first = "number1";
let second = "number2";
let numbers = [first, second];
console.log(numbers);

Getting the data

To get the data from an array, must you call the name, but if you want a specific element in the array, you must use the index number.

let fruits = ['Apple', 'Banana'];
console.log(fruits);
let weekend = ['friday', 'sunday', 'saturday'];
console.log(weekend[0]);
let food = [["eggs", "ham"], ["bacon", "meat"], ["ice cream", "cake"]];
console.log(food[0]);
let breakfast = ['eggs', 'bacon', 'milk'];
console.log(breakfast.length);

Array in use

You can use array in many ways.

let breakfast = ['eggs', 'bacon', 'milk'];
console.log(breakfast.length);

Array methods

There is some array methods

array.push();

array.push(item1, item2, ..., itemX);

.push(); is a method that adds new items to the end of an array, and returns the new length.

let breakfast = ['eggs', 'bacon', 'milk'];
breakfast.push("Juice");
console.log(breakfast);

array.pop();

array.pop();

.pop(); is a method that removes the last element of an array, and returns that element.

let breakfast = ['eggs', 'bacon', 'milk'];
breakfast.pop();
console.log(breakfast);

array.splice();

array.splice(index, howmany, item1, ....., itemX);

.splice(); is a method that adds/removes items to/from an array, and returns the removed. But it can also replace an item.

let breakfast = ['eggs', 'bacon', 'milk'];
breakfast.splice(0,1);
console.log(breakfast);
let breakfast = ['eggs', 'bacon', 'milk'];
breakfast.splice(1,1, "Juice");
console.log(breakfast);

array.shift();

array.shift(itemX);

.shift(); is a method that method removes the first item of an array.

let breakfast = ['eggs', 'bacon', 'milk'];
breakfast.shift("eggs");
console.log(breakfast);

array.unshift();

array.unshift(item1, item2, ..., itemX);

.unshift(); is a method that adds new items to the beginning of an array, and returns the new length.

let breakfast = ['eggs', 'bacon', 'milk'];
breakfast.unshift("apples");
console.log(breakfast);