JavaScript Array object
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).
Creating an 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 a literal (recommended):
let arr = [1, 2, 3]; // 创建一个包含 1, 2, 3 的数组
The first array element has an index of 0, the second has an index of 1, and so on.
For more about JavaScript Array, please refer to JavaScript 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 the array. |
| prototype | Allows you to add properties or methods to the array object. |
Array object methods
| Method | Description |
|---|---|
| [...] | Creates a new array. |
| concat() | Joins two or more arrays and returns the result. |
| copyWithin() | Copies elements from one specified position of an array to another specified position in the array. |
| entries() | Returns an iterable object for the array. |
| every() | Tests whether every element in the array meets the condition. |
| fill() | Fills the array with a fixed value. |
| filter() | Tests the elements and returns an array of all elements that meet the condition. |
| find() | Returns the array elements that meet the condition passed in as a test (function). |
| findIndex() | Returns the index of the array element that meets the condition passed in as a test (function). |
| forEach() | Executes a callback function once for each element of the array. |
| from() | Creates an array from the given object. |
| fromAsync() | Creates a new array from an async iterable, an iterable, or an 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() | Joins all elements of an array into a string. |
| keys() | Returns an iterable of the array's keys. |
| map() | Processes each element of the array with a specified function and returns the resulting array. |
| pop() | Removes the last element of the array and returns the removed element. |
| push() | Adds one or more elements to the end of the array and returns the new length. |
| reduce() | Reduces the array elements to a single value (from left to right). |
| reduceRight() | Reduces the array elements to a single value (from right to left). |
| reverse() | Reverses the order of the elements in the array. |
| shift() | Removes and returns the first element of the array. |
| slice() | Selects a part of the array and returns a new array. |
| some() | Tests whether any element in the array meets the specified condition. |
| sort() | Sorts the elements of the array. |
| splice() | Adds or removes elements from the array. |
| toString() | Converts the array to a string and returns the result. |
| toLocaleString() | Formats each element of the array according to the language setting (locale) of the current environment. |
| unshift() | Adds one or more elements to the beginning of the array and returns the new length. |
| valueOf() | Returns the primitive value of the array object. |
| Array.of() | Converts a set of values to an array. |
| Array.at() | Accepts an integer value and returns the element at that index, allowing positive and negative numbers. A negative integer counts back from the last element in the array. |
| Array.flat() | Creates a new array composed of the return values of calling the provided function once for each element of the original array. |
| Array.flatMap() | Maps each element using a mapping function, then flattens the result into a new array. |
| Array.with() | Updates array elements. |
| Array.findLastIndex() | Executes a function on each element of the array. |
| Array.lastIndexOf() | Executes a function on each element of the array. |
| Array.findLast() | Executes a function on each element of the array. |
| Array.toReversed() | Reverses the order of the elements in the array. |
| Array.toSorted() | Sorts the elements of the array in alphabetical order. |
| Array.toSpliced() | Adds and/or removes elements from the array. |
| [Symbol.iterator](jsref-array-symbol-iterator.html) | Returns an array iterator object that yields the value of each index in the array. |