Like most languages, JavaScript lets you create arrays to store bunches of values in. An array is simply a sequence, or list, of values. A value in a JavaScript array can be anything from a number or string, through to a function or an object; JavaScript arrays can store pretty much any type of data. A single value in a JavaScript array is called an element.
With JavaScript arrays, you can:
- Store any number of values, including no values at all (known as an empty array)
- Access any or all the elements of an array through a single variable name
- Read and write elements of an array
- Loop through all the elements of an array
- Join two or more arrays together to make one longer array
- Convert an array to one long string
- Add or remove elements from the beginning, end, or middle of an array
- Reverse the order of the elements in an array
- Sort the elements of an array in any order you like
- Nest arrays within other arrays
JavaScript stores arrays as Array
objects, so most of the things you can do with arrays revolve around the properties and methods of the Array
class.
In this introductory article, you look at creating arrays, accessing the contents of arrays, the concept of array lengths, and looping through arrays.