JavaScript Cheet Sheet

1.8 - Array Object

Example 1

const cars1 = ["Kia", "Ford", "Toyota"];
// Demonstrating length property
example1Length.innerText = "Length of cars1 array is: " +cars1.length;
// toString() Method converts the array elements to a string
example1Elements.innerText = cars1.toString();

Example 1 Output

Click button to show result...

Example 2 - push()

const cars1 = ["Kia", "Ford", "Toyota"];
// Demonstrating push() method to add an element to an array cars1.push("Nissan");
example2Length.innerText = "Length of cars1 array is: " +cars1.length;
example2Elements.innerText = cars1.toString();

Example 2 Output

Click button to show result...

Example 3 - splice()

const cars1 = ["Kia", "Ford", "Toyota"];
// Demonstrating splice() method to remove second element and replace with "Nissan"
cars1.splice(1,1,"Nissan");
example3Length.innerText = "Length of cars1 array is: " +cars1.length;
example3Elements.innerText = cars1.toString();

Example 3 Output

Click button to show result...

Example 4 - concat()

const cars1 = ["Kia", "Ford", "Toyota"];
const cars2 = ["GM", "Chrysler", "Volkswagon"];
// Demonstrating concat() method to combine two arrays into one larger array. Method is used with one array and the other array is passed as a parameter.
const newArray = cars1.concat(cars2);
example4Length.innerText = "Length of newArray array is: " +newArray.length;
example4Elements.innerText = newArray.toString();

Example 4 Output

Click button to show result...

Example 5 - slice()

const cars1 = ["Kia", "Ford", "Toyota", "BMW", "Honda", "Fiat"];
// Demonstrating slice() method to take a portion of an array and copy it to a new array variable. The example below copies the second and third element from cars1 array to newArray.
const newArray = cars1.slice(1,4);
example5Length.innerText = "Length of newArray array is: " +newArray.length;
example5Elements.innerText = newArray.toString();

Example 5 Output

Click button to show result...

Example 6 - join()

const cars1 = ["Kia", "Ford", "Toyota", "BMW", "Honda", "Fiat"];
// Demonstrating join() method which joins elements of an array into a string adding a separator of your choice which you enter as a parameter.
var carsJoined = cars1.join("!!! ");
example6Elements.innerText = carsJoined;

Example 6 Output

Click button to show result...

Example 7 - sort()

const cars1 = ["Kia", "Ford", "Toyota", "BMW", "Honda", "Fiat"];
// Demonstrating sort() method which sorts elements of an array into alphabetical or numerical order.
const carsSorted = cars1.sort();
example7Elements.innerText = carsSorted.toString();

Example 7 Output

Click button to show result...

Example 8 - reverse()

const cars1 = ["Kia", "Ford", "Toyota", "BMW", "Honda", "Fiat"];
// Demonstrating reverse() method reverses the elements of an array. The first element becomes the last element.
const carsSorted = cars1.reverse();
example8Elements.innerText = carsSorted.toString();

Example 8 Output

Click button to show result...

Example 9 - indexOf()

const cars1 = ["Kia", "Ford", "Toyota", "BMW", "Honda", "Fiat"];
// Demonstrating indexOf() method which returns the index of the elements first occurrence.
example8Elements.innerText = cars1.indexOf("Toyota");

Example 9 Output

Click button to show result...