3.6 Array Methods
syntax: .toString()
examples:
let colors = ["Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet"];
console.log("The colors that makeup the rainbow are " + colors.toString());
Console view:
syntax: .join()
examples:
colors = ["Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet"];
console.log("The colors that makeup the rainbow are " + colors.join(" and "));
Console view:
syntax: .push()
examples:
colors = ["Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet"];
colors.push("White");
colors.push("Black");
console.log(
"New colors were added to the list : " + colors[7] + " and " + colors[8]
);
Console view:
syntax: .shift()
examples:
colors = ["Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet"];
console.log("The first color in the list is : " + colors.shift());
console.log(
"We removed Red from the list, so now the colors are " + colors.toString()
);
Console view:
syntax: .unshift()
examples:
colors = ["Orange", "Yellow", "Green", "Blue", "Indigo", "Violet"];
console.log("I'm adding Red to the front of the array ");
colors.unshift("Red");
console.log("Now the colors are back to normal : " + colors.toString());
Console view:
syntax: .sort()
examples:
let letters = ["P", "M", "E", "T", "N", "X", "J", "I"];
console.log("Letters unsorted: " + letters.toString());
console.log("Letters sorted: " + letters.sort().toString());
Console view:
syntax: .reverse()
examples:
let order = ["First", "Second", "Third", "Fourth", "Fifth"];
console.log("Original order: " + order.toString());
console.log("Reversed order: " + order.reverse().toString());
Console view:
See the Pen Exercise 3.6 by LSU DDEM (@lsuddem) on CodePen.