JavaScript ArrayObject


The Array object in JavaScript is a special type of object used to store multiple values.

Array stores elements in order, and they can be accessed by index (starting from 0).

Create Array

Arrays can be created in several ways:

Using the Array constructor:

let arr1 = new Array(3);  // 创建一个长度为 3 的空数组
let arr2 = new Array(1, 2, 3);  // 创建一个包含 1, 2, 3 的数组

Using literals (recommended):

let arr = [1, 2, 3];  // 创建一个包含 1, 2, 3 的数组

The index of the first array element is 0, the second is 1, and so on.

For more information about JavaScript Array, please refer toJavaScript Array Object。


Array Properties

Property Description
constructor Returns the prototype function that created the array object.
length Sets or returns the number of elements in an array.
prototype Allows you to add properties or methods to an array object.

Array Object Methods

Method Description
[...] Creates a new array.
concat() Concatenates two or more arrays and returns the result.
copyWithin() Copies elements from a specified position in the array to another specified position in the array.
entries() Returns an iterable object of the array.
every() Tests whether every element in the array meets the condition.
fill() Fills the array with a fixed value.
filter() Tests numeric elements and returns an array of all elements that meet the condition.
find() Returns the array element that meets the condition of the passed test (function).
findIndex() Returns the index of the array element that meets the condition of the passed test (function).
forEach() Executes a callback function once for each element in the array.
from() Creates an array from a given object.
fromAsync() Creates a new array from an async iterable, iterable, or array-like object.
includes() Determines whether an array contains a specified value.
indexOf() Searches for an element in the array and returns its position.
isArray() Determines whether an object is an array.
join() Places all elements of an array into a string.
keys() Returns an iterable object of the array, containing the keys of the original array.
map() Processes each element of the array with the specified function and returns the processed array.
pop() Removes the last element of an array and returns the removed element.
push() Adds one or more elements to the end of an array and returns the new length.
reduce() Computes array elements into a value (from left to right).
reduceRight() Computes array elements into a value (from right to left).
reverse() Reverses the order of elements in an array.
shift() Removes and returns the first element of an array.
slice() Selects a portion of an array and returns a new array.
some() Tests whether any element in the array meets the specified condition.
sort() Sorts the elements of an array.
splice() Adds or removes elements from an array.
toString() Converts an array to a string and returns the result.
toLocaleString() Formats each element in the array according to the current environment's language settings (locale).
unshift() Adds one or more elements to the beginning of an array and returns the new length.
valueOf() Returns the primitive value of the array object.
Array.of() Converts a set of values into an array.
Array.at() Used to receive an integer value and return the element corresponding to that index, allowing positive and negative numbers. Negative integers count backward from the last element in the array.
Array.flat() Creates a new array consisting of the return values from calling the provided function once on each element in the original array.
Array.flatMap() Maps each element using a mapping function, then compresses the results into a new array.
Array.with() Updates array elements.
Array.findLastIndex() Executes a function on each element in the array.
Array.lastIndexOf() Executes a function on each element in the array.
Array.findLast() Executes a function on each element in the array.
Array.toReversed() Reverses the order of elements in the array.
Array.toSorted() Sorts the elements in the array in alphabetical order.
Array.toSpliced() Adds and/or removes elements from an array.
[Symbol.iterator]() Returns an array iterator object that produces the value at each index in the array.
Other Extensions