JavaScript Array Object and Method

JavaScript Array object

The Array object is used store multiple values in a single variable.

Example:

var fruits = new Array("apple","orange","mango");

The Array parameter is a list of strings or integers. The maximum length allowed for an array is 4,294,967,295.

Array Properties

  • Inext - The property represents the zero-based index of the match in the string.
  • Index - This property is only present in arrays created by regular expression matches.
  • Length - Reflects the number of elements in an array.
  • Prototype - The prototype property allows you to add properties and methods to an object.

Array Methods

Method Description
concat() Returns new array by combining values of an array that is specified as parameter with existing array values.
every() Returns true or false if every element in the specified array satisfies a condition specified in the callback function. Returns false even if single element does not satisfy the condition.
filter() Returns a new array with all the elements that satisfy a condition specified in the callback function.
forEach() Executes a callback function for each elements of an array.
indexOf() Returns the index of the first occurrence of the specified element in the array, or -1 if it is not found.
join() Returns string of all the elements separated by the specified separator
lastIndexOf() Returns the index of the last occurrence of the specified element in the array, or -1 if it is not found.
map() Creates a new array with the results of calling a provided function on every element in this array.
pop() Removes the last element from an array and returns that element.
push() Adds one or more elements at the end of an array and returns the new length of the array.
reduce() Pass two elements simultaneously in the callback function (till it reaches the last element) and returns a single value.
reduceRight() Pass two elements simultaneously in the callback function from right-to-left (till it reaches the last element) and returns a single value.
reverse() Reverses the elements of an array. Element at last index will be first and element at 0 index will be last.
shift() Removes the first element from an array and returns that element.
slice() Returns a new array with specified start to end elements.
some() Returns true if at least one element in this array satisfies the condition in the callback function.
sort() Sorts the elements of an array.
splice() Adds and/or removes elements from an array.
toString() Returns a string representing the array and its elements.
unshift() Adds one or more elements to the front of an array and returns the new length of the array.


Read Also: