This was the first contribution I’ve ever made at GitHub, belonging to the original learn.jquery.com website. The original article is now offline, but saved here for the future.
Arrays are zero-indexed, ordered lists of values. They are a handy way to store a set of related items of the same type (such as strings), though in reality, an array can include multiple types of items, including other arrays.
To create an array you can either use the object constructor or the literal declaration, by assigning your variable a list of values right after the declaration.
The literal declaration is preferred, see the Google Coding Guidelines for more information.
If you don’t know your values yet, it is also possible to declare an empty Array, and add elements either through functions or through accessing by index:
‘push’ is a function which adds an element on the end of the array and expands the array respectively. You also can directly add items by index. Missing indices will be filled with ‘undefined’;
So ‘push’ is far more safe, especially if you don’t know the size of your array yet. With the index you not only assign values to array items, but also access those.
Array methods and properties
The ‘length’ property is used to know the amount of items in your array.
You will need the length property for looping through an array:
Except when you are using for … in loops:
With ‘concat’, you can concatenate two or more arrays
‘join’ creates a string representation of your array. It’s parameter is as string which works as a seperator between elements (default is a comma);
‘pop’ removes the last element of an array. It is the opposite method to ‘push’
As the name suggests, the elements of the array are in reverse order after calling this method
Removes the first element of an array. With ‘pop’ and ‘shift’ you can recreate the method of a queue
Extracts a part of the array and returns them in a new one. This method takes one parameter, which is the starting index.
Removes a certain amount of elements and adds new ones at the given index. It takes at least 3 parameters
- idx = the starting index
- len = the number of elements to remove
- values = the values which should be inserted at idx
Sorts an array. It takes one parameter, which is a comparing function. If this function is not given, the array is sorted ascending
The return value of descending (for this example) is important. If the return value is less than zero, the index of a is before b, and if it is greater than zero it’s vice-versa. If the return value is zero, the elements index is equal.
Inserts an element at the first position of the array
In modern browsers, like Chrome, Firefox and Internet Explorer 9 it is possible to traverse through arrays by a so called ‘forEach’ method, where you pass a function which is called for each element in your array.
The function takes up to three arguments:
- element - The element itself
- index - The index of this element in the array
- array - The array itself
All of the are optional, but you will need at least the ‘element’ parameter in most cases.
Me again. The Gulp, Yeoman, Bower book is pretty sweet. Just saying.
Comments? Shoot me a tweet!