Array Reference
Array Object:
The Array object is used to store multiple values in a single variable. Array indexes are zero-based: The first element in the array is 0, the second is 1, and so on.
Creating an Array:
Using an array literal is the easiest way to create a JavaScript Array.
Syntax:
var array-name = [item1, item2, ...];
Example:
var IDs = [1, 2, 3, 4, 5];
Access the Elements of an Array:
You refer to an array element by referring to the index number. This statement accesses the value of the first element in IDs:
var id = ID[0];
This statement modifies the first element in IDs:
cars[0] = 1;
Arrays are Objects:
Arrays are a special type of objects. The typeof operator in JavaScript returns "object" for arrays. But, JavaScript arrays are best described as arrays. Arrays use numbers to access its "elements". In this example, IDs[1] returns 2:
Example:
var material = ["Elastic", "Clay", 2000];
Array Properties:
|
Property
|
Description
|
|
constructor
|
Returns the function that created the Array object's prototype
|
|
length
|
Sets or returns the number of elements in an array
|
|
prototype
|
Allows you to add properties and methods to an Array object
|
Array Methods:
|
Method
|
Description
|
|
concat()
|
Joins two or more arrays, and returns a copy of the joined arrays
|
|
copyWithin()
|
Copies array elements within the array, to and from specified positions
|
|
every()
|
Checks if every element in an array pass a test
|
|
fill()
|
Fill the elements in an array with a static value
|
|
filter()
|
Creates a new array with every element in an array that pass a test
|
|
find()
|
Returns the value of the first element in an array that pass a test
|
|
findIndex()
|
Returns the index of the first element in an array that pass a test
|
|
forEach()
|
Calls a function for each array element
|
|
indexOf()
|
Search the array for an element and returns its position
|
|
isArray()
|
Checks whether an object is an array
|
|
join()
|
Joins all elements of an array into a string
|
|
lastIndexOf()
|
Search the array for an element, starting at the end, and returns its position
|
|
map()
|
Creates a new array with the result of calling a function for each array element
|
|
pop()
|
Removes the last element of an array, and returns that element
|
|
push()
|
Adds new elements to the end of an array, and returns the new length
|
|
reduce()
|
Reduce the values of an array to a single value (going left-to-right)
|
|
reduceRight()
|
Reduce the values of an array to a single value (going right-to-left)
|
|
reverse()
|
Reverses the order of the elements in an array
|
|
shift()
|
Removes the first element of an array, and returns that element
|
|
slice()
|
Selects a part of an array, and returns the new array
|
|
some()
|
Checks if any of the elements in an array pass a test
|
|
sort()
|
Sorts the elements of an array
|
|
splice()
|
Adds/Removes elements from an array
|
|
toString()
|
Converts an array to a string, and returns the result
|
|
unshift()
|
Adds new elements to the beginning of an array, and returns the new length
|
|
valueOf()
|
Returns the primitive value of an array
|
Example:
var names = ["Sand", "Clay", "Silt", "Null"];
names.length; // the length of names is 4
names.push("Gravel"); // adds a new element (Gravel) to names
names[6] = "Gravel"; // adds a new element (Gravel) to names
var y = names.sort(); // The sort() method sorts arrays
names[names.length] = "Gravel"; // adds a new element (Gravel) to names