In computer science, Arrays are both a data structure and a data type. According to Wikipedia,
an array data structure, or simply an array, is a data structure consisting of a collection of elements (values or variables), each identified by at least one array index or key.
Lets focus on a couple key portions of this definition here. First,
a collection of elements and
each identified by at least one array index or key. In that sense what we have then is a data structure that is a collection of elements identified by at least one index.
Arrays are list-like objects whose prototype has methods to perform traversal and mutation operations.
const myArray = [ 0, "apple", [ 0, 1 ] ]; const newArray = new Array( 0, "apple", [ 0, 1 ] );
const myArray = [ 0, "apple", [ 0, 1 ] ]; console.log(myArray); // => apple console.log(myArray); // => [ 0, 1 ]
const myArray = [ 0, "apple", [ 0, 1 ] ]; typeof(myArray); // => object
Although our arrays are objects, it would be more accurate to describe them as special objects which function as you would expect for an array.
There are many different built-in methods for arrays such as
pop, etc. For now let's just take a look at length, push, and pop.
length method of our array will return an integer representing the number of elements within an array. The push and pop methods both deal with the end of the array. Push will allow us to add an element to the end of an array and then return the length of the array. Pop takes the opposite action, removing the last element from an array but it instead returns the removed element.
const myArray = [ 0, "apple", [ 0, 1 ] ]; myArray.length; // => 3 myArray.push(15); // => 4 myArray.length; // => 4 myArray.pop(); // => 15