JavaScript Array Object
These tutorials introduce the Array object, one of the most flexible data types JavaScript offers. Arrays have a dynamic size, can contain elements of different types, and support a number of useful methods (like on-the-fly sorting).
What is an Array?
A JavaScript array is a collection of variables ("elements") referenced by the array name and the zero-based index of their position: "MyArray[0]
" points to the first array element of MyArray
.
Each array element is an independent variable, which can be of a different data type than other elements in the same array. Below, we declare a JavaScript array containing four elements: each is of a different data type.
var arlene = new Array("a string", 12, 47.845, true);
This perfectly valid JavaScript array contains as elements a string, an integer, a floating point number, and a boolean (a special JavaScript object).
Types of Arrays in JavaScript
Arrays are one-dimensional by default, but JavaScript also supports multi-dimensional arrays. A multidimensional array is an array of arrays: each element is itself an array. In later tutorials, you will learn how to create and use both one-dimensional and multidimensional arrays.
JavaScript also supports "associative arrays": by default, array elements are referenced through an auto-incremented index handled by the JavaScript interpreter. But you can create custom array referencing systems.
JavaScript Array Properties
Our tutorial will cover the three properties available to JavaScript arrays: length, constructor, and prototype. Length is the property that allows you at any time in your scripts to determine the number of available spots in an array - whether the spots have been initialized or not with an element.
- The length property returns the number of elements in an array.
- The constructor property points to the function used to create the array.
- The prototype property gives you access to the native Array data type.
JavaScript Array Methods
Our tutorials will cover the most common JavaScript array methods. Methods are special functions available to different types of objects; like other objects, JavaScript arrays have numerous manipulation and conversion methods.
- The concat() method returns an array incorporating new elements passed.
- The join() method returns array elements joined by a string.
- The pop() method deletes and returns the last element of an array.
- The push() method appends its arguments at the end of an array.
- The reverse() method reverses the order of array elements (without sorting).
- The shift() method deletes and returns the first element of an array.
- The slice() method returns a sub-array based on position indexes supplied.
- The sort() method sorts array elements alpha-numerically.
- The sort() method's custom sorting function allows to customize sorting order.
- Sort array elements in random order (shuffle) with an array randomizer function.
- The splice() method lets you to delete, add, and substitute array elements.
- The toString() method returns a comma-separated string representation of arrays.
- The unshift() method inserts its arguments at the start of an array.