BASIC ARRAY METHODS
Array.length
let fruits = [‘apple’, ‘banana’, ‘cherry’, ‘date’, ‘elderberry’]
let result = fruits.length;
console.log(result);
Array.toString()
let fruits = [‘apple’, ‘banana’, ‘cherry’, ‘date’, ‘elderberry’]
let result = fruits.toString();
console.log(result);
Array.at(index)
let fruits = [‘apple’, ‘banana’, ‘cherry’, ‘date’, ‘elderberry’]
let result = fruits.at(2);
console.log(result);
Array.join(‘whatever’)
let fruits = [‘apple’, ‘banana’, ‘cherry’, ‘date’, ‘elderberry’];
let result = fruits.join(‘-‘)
console.log(result); // => apple-banana-cherry-date-elderberry
Array.pop()
let fruits = [‘apple’, ‘banana’, ‘cherry’, ‘date’, ‘elderberry’];
let result = fruits.pop()
console.log(result); // => elderberry
console.log(fruits); // => [‘apple’, ‘banana’, ‘cherry’, ‘date’];
Array.push()
let fruits = [‘apple’, ‘banana’, ‘cherry’, ‘date’, ‘elderberry’];
let result = fruits.push(‘kiwi’);
console.log(result); // => 6 ! returns the length
console.log(fruits); // => [‘apple’, ‘banana’, ‘cherry’, ‘date’, ‘elderberry’, ‘kiwi’]
Array.shift()
let fruits = [‘apple’, ‘banana’, ‘cherry’, ‘date’, ‘elderberry’];
let result = fruits.shift();
console.log(result); // => apple
Array.unshift()
let fruits = [‘apple’, ‘banana’, ‘cherry’, ‘date’, ‘elderberry’];
let result = fruits.unshift(‘pear’);
console.log(result); // => apple
console.log(fruits); // => [‘pear’, ‘apple’, ‘banana’, ‘cherry’, ‘date’, ‘elderberry’]
delete array[index]
let fruits = [‘apple’, ‘banana’, ‘cherry’, ‘date’, ‘elderberry’];
delete fruits[1];
console.log(fruits); // => [‘apple’, empty, ‘banana’, ‘cherry’, ‘date’, ‘elderberry’]
concat()
const myGirls = [“Cecilie”, “Lone”];
const myBoys = [“Emil”, “Tobias”, “Linus”];
const myChildren = myGirls.concat(myBoys);
array.flat()
const myArr = [[1,2],[3,4],[5,6]];
const newArr = myArr.flat();
console.log(newArr); // => [1,2,3,4,5,6]
array.splice()
fruits.splice(2, 0, ‘kiwi’) // => [‘apple’, ‘banana’, ‘kiwi’, ‘date’, ‘elderberry’];
console.log(fruits);
array.slice()
let fruits = [‘apple’, ‘banana’, ‘cherry’, ‘date’, ‘elderberry’];
let result = fruits.slice(2, 3) // => [‘cherry’]
console.log(result);