- Arrays start at 0. The first element is indexed as 0, the second as 1, and so on.
- Arrays can hold a set of any values: numbers, words or objects.
Definition and Purpose
For instance, an array called
cars could consist of different automobile manufacturers like Audi, Mazda, and Volvo. The following code illustrates this example and reveals the syntax of an array:
var cars = ["Audi", "Mazda", "Volvo"];
Another frequently used method is the
For more information about the standard methods for dealing with arrays, continue reading this tutorial!
constructor property. It is also possible to receive information about the length of arrays by using
prototype property is used when developers want to add properties and methods to array objects.
|constructor||Returns a function which was used in creating the array's prototype.|
|length||Can be used to return and set the number of elements the array holds.|
|prototype||Lets you add new methods and properties to the array object.|
Methods to Use
For instance, the
array.shift()). It is used when it is necessary to delete the first element in an array. Furthermore, the removed element will be delivered as the return value.
array.unshift() method might seem similar to the previous function, but it actually has an opposite functionality. Unshifting arrays means adding an element (or a few) to the beginning of an array.
|concat()||Joins multiple arrays. Returns a new array.|
|copyWithin()||Copies elements from the array from one index to another.|
|every()||Checks if all the elements of an array pass a test.|
|fill()||Fills an array with some values.|
|filter()||Creates a new array using every element that passes a test.|
|find()||Creates a new array with all the elements that pass a test implemented by the provided function.|
|findIndex()||Returns the index of the first element which passes a test.|
|forEach()||Executes some code to each element of an array.|
|indexOf()||Returns the index of a specified element.|
|isArray()||Checks whether a specified element is in the array.|
|join()||Puts array elements into a string list.|
|lastIndexOf()||Returns an index of the specified element. Starts searching from the end of an array.|
|map()||Executes a function for each element of an array and stores the result in a new array.|
|pop()||Removes and returns the last element for the array.|
|push()||Adds a new value to the end of an array and returns the new length of the array.|
|reduce()||Reduces the values of elements in an array to a single value.|
|reduceRight()||Reduces the values of elements in an array to a single value (starting from the right).|
|reverse()||Reverses the order of the elements in an array.|
|shift()||Removes and returns the first element from an array.|
|slice()||Takes a part of an array and forms a new array.|
|some()||Checks if at least one of the elements passes a test.|
|sort()||Sorts the elements of an array alphabetically.|
|splice()||Can be used to add or remove items from an array.|
|toString()||Puts array elements into a string list. Returns the list.|
|unshift()||Adds a new element to an array (at the beginning), and unshifts older elements (increases their index by one). Returns the new array length.|
|valueOf()||Returns an array as a primitive value (usually a string).|