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();
Click button to show result...
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();
Click button to show result...
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();
Click button to show result...
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();
Click button to show result...
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();
Click button to show result...
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;
Click button to show result...
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();
Click button to show result...
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();
Click button to show result...
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");
Click button to show result...