var myArray = [
“Frank”,
“Paul”,
“Peter”,
“Adam”
];
/* Sorts the array alphabetically, then removes the last item, which gets set as the value of the x variable */
var x = myArray.sort().pop();
console.log(x); // Logs “Peter” to the console
A large source of confusion for new programmers is around the differences between arrays and objects and when to use either of them. The answer is very simple. The two are largely identical, with one major difference. That difference is that arrays use numbers as their indexes, while objects use names as their indexes. What this really means is that when you are working with very structured data, an object is going to be better, while working with something a little simpler and linear, an array is going to be better.
A shopping list, for example, would lend itself well to an array, while details about a car would be much better suited to an object where we can detail the specific values and assign them to a name.