| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Array | [Back] [Original] |
Get to know MDN better
This feature is well established and works across many devices and browser versions. Its been available across browsers since 20157.
* Some parts of this feature may have varying levels of support.
JavaScript Array high-levellist-like Javascript
[element0, element1, ..., elementN]
new Array(element0, element1[, ...[, elementN]])
new Array(arrayLength)
elementN Array arrayLength JavaScript Array JavaScript Array Literals
arrayLength Array 0 2^32 - 1 JavaScript RangeError
ArraylistObjectPrototypemethodsJavaScript typed arrays
JavaScript zero-indexed 0 length 1
var arr = ["this is the first element", "this is the second element"];
console.log(arr[0]); // 'this is the first element'
console.log(arr[1]); // 'this is the second element'
console.log(arr[arr.length - 1]); // 'this is the second element'
Array toString
console.log(arr.0); //
JavaScript 3d
var years = [1950, 1960, 1970, 1980, 1990, 2000, 2010];
console.log(years.0); //
console.log(years[0]); //
renderer.3d.setTexture(model, 'character.png'); //
renderer['3d'].setTexture(model, 'character.png'); //
'3d' 3d JavaScript years['2'] years[2]JavaScript toString years[2] 2 '2' '02' years true
console.log(years["2"] != years["02"]);
var promise = {
var: "text",
array: [1, 2, 3, 4],
};
console.log(promise["var"]);
length JavaScript length length joinsliceindexOf length pushsplice
var fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
indexJavaScript length
fruits[5] = "mango";
console.log(fruits[5]); // 'mango'
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 6
fruits.length = 10;
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 10
fruits.length = 2;
console.log(Object.keys(fruits)); // ['0', '1']
console.log(fruits.length); // 2
javascript RegExp.exec, String.match, String.replace
// d b d
// Remember matched b's and the following d
//
var myRe = /d(b+)(d)/i;
var myArray = myRe.exec("cdbBdbsbz");
| / | ||
|---|---|---|
input |
cdbBdbsbz | |
index |
( 0 ) | 1 |
[0] |
dbBd | |
[1], ...[n] |
Read-only elements that specify the parenthesized substring matches, if included in the regular expression. The number of possible parenthesized substrings is unlimited. | [1]: bB [2]: d |
Array 1
Array[Symbol.species]ArrayArray.from() Array
Array.isArray()true false
Array.of() Array
Array Array (Array constructor) (prototype object)
Array.prototype.lengthReflects the number of elements in an array.
Array.prototype[Symbol.unscopables]Contains property names that were not included in the ECMAScript standard prior to the ES2015 version and that are ignored for with statement-binding purposes.
Array.prototype.at()Returns the array item at the given index. Accepts negative integers, which count back from the last item.
Array.prototype.concat()Returns a new array that is the calling array joined with other array(s) and/or value(s).
Array.prototype.copyWithin()Copies a sequence of array elements within an array.
Array.prototype.entries()Returns a new array iterator object that contains the key/value pairs for each index in an array.
Array.prototype.every()Returns true if every element in the calling array satisfies the testing function.
Array.prototype.fill()Fills all the elements of an array from a start index to an end index with a static value.
Array.prototype.filter()Returns a new array containing all elements of the calling array for which the provided filtering function returns true.
Array.prototype.find()Returns the value of the first element in the array that satisfies the provided testing function, or undefined if no appropriate element is found.
Array.prototype.findIndex()Returns the index of the first element in the array that satisfies the provided testing function, or -1 if no appropriate element was found.
Array.prototype.findLast()Returns the value of the last element in the array that satisfies the provided testing function, or undefined if no appropriate element is found.
Array.prototype.findLastIndex()Returns the index of the last element in the array that satisfies the provided testing function, or -1 if no appropriate element was found.
Array.prototype.flat()Returns a new array with all sub-array elements concatenated into it recursively up to the specified depth.
Array.prototype.flatMap()Returns a new array formed by applying a given callback function to each element of the calling array, and then flattening the result by one level.
Array.prototype.forEach()Calls a function for each element in the calling array.
Array.prototype.group() Groups the elements of an array into an object according to the strings returned by a test function.
Array.prototype.groupToMap() Groups the elements of an array into a Map according to values returned by a test function.
Array.prototype.includes()Determines whether the calling array contains a value, returning true or false as appropriate.
Array.prototype.indexOf()Returns the first (least) index at which a given element can be found in the calling array.
Array.prototype.join()Joins all elements of an array into a string.
Array.prototype.keys()Returns a new array iterator that contains the keys for each index in the calling array.
Array.prototype.lastIndexOf()Returns the last (greatest) index at which a given element can be found in the calling array, or -1 if none is found.
Array.prototype.map()Returns a new array containing the results of invoking a function on every element in the calling array.
Array.prototype.pop()Removes the last element from an array and returns that element.
Array.prototype.push()Adds one or more elements to the end of an array, and returns the new length of the array.
Array.prototype.reduce()Executes a user-supplied "reducer" callback function on each element of the array (from left to right), to reduce it to a single value.
Array.prototype.reduceRight()Executes a user-supplied "reducer" callback function on each element of the array (from right to left), to reduce it to a single value.
Array.prototype.reverse()Reverses the order of the elements of an array in place. (First becomes the last, last becomes first.)
Array.prototype.shift()Removes the first element from an array and returns that element.
Array.prototype.slice()Extracts a section of the calling array and returns a new array.
Array.prototype.some()Returns true if at least one element in the calling array satisfies the provided testing function.
Array.prototype.sort()Sorts the elements of an array in place and returns the array.
Array.prototype.splice()Adds and/or removes elements from an array.
Array.prototype.toLocaleString()Returns a localized string representing the calling array and its elements. Overrides the Object.prototype.toLocaleString() method.
Array.prototype.toString()Returns a string representing the calling array and its elements. Overrides the Object.prototype.toString() method.
Array.prototype.unshift()Adds one or more elements to the front of an array, and returns the new length of the array.
Array.prototype.values()Returns a new array iterator object that contains the values for each index in the array.
Array.prototype[Symbol.iterator]()An alias for the values() method by default.
0 msgArray msgArray[0] msgArray[99] 100
var msgArray = [];
msgArray[0] = "Hello";
msgArray[99] = "world";
if (msgArray.length === 100) {
console.log("The length is 100.");
}
var first = fruits[0];
// Apple
var last = fruits[fruits.length - 1];
// Banana
fruits.forEach(function (item, index, array) {
console.log(item, index);
});
// Apple 0
// Banana 1
var newLength = fruits.push("Orange");
// ["Apple", "Banana", "Orange"]
var last = fruits.pop(); // Orange
// ["Apple", "Banana"];
var first = fruits.shift(); // Apple
// ["Banana"];
var newLength = fruits.unshift("Strawberry"); //
// ["Strawberry", "Banana"];
fruits.push("Mango");
// ["Strawberry", "Banana", "Mango"]
var pos = fruits.indexOf("Banana");
// 1
var removedItem = fruits.splice(pos, 1); // pos 1
// ["Strawberry", "Mango"]
var vegetables = ["Cabbage", "Turnip", "Radish", "Carrot"];
console.log(vegetables);
// ["Cabbage", "Turnip", "Radish", "Carrot"]
var pos = 1,
n = 2;
var removedItems = vegetables.splice(pos, n);
//
// n
console.log(vegetables);
// ["Cabbage", "Carrot"]
console.log(removedItems);
// ["Turnip", "Radish"]
var shallowCopy = fruits.slice(); //
// ["Strawberry", "Mango"]
'p' (6,4) (4,4) (6,4)
var board = [
["R", "N", "B", "Q", "K", "B", "N", "R"],
["P", "P", "P", "P", "P", "P", "P", "P"],
[" ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " "],
["p", "p", "p", "p", "p", "p", "p", "p"],
["r", "n", "b", "q", "k", "b", "n", "r"],
];
console.log(board.join("\n") + "\n\n");
//
board[4][4] = board[6][4];
board[6][4] = " ";
console.log(board.join("\n"));
R,N,B,Q,K,B,N,R P,P,P,P,P,P,P,P , , , , , , , , , , , , , , , , , , , , , , , , , , , , p,p,p,p,p,p,p,p r,n,b,q,k,b,n,r R,N,B,Q,K,B,N,R P,P,P,P,P,P,P,P , , , , , , , , , , , , , , , , , ,p, , , , , , , , , , p,p,p,p, ,p,p,p r,n,b,q,k,b,n,r
values = [];
for (var x = 0; x < 10; x++) {
values.push([2 ** x, 2 * x ** 2]);
}
console.table(values);
0 1 0 1 2 2 2 4 8 3 8 18 4 16 32 5 32 50 6 64 72 7 128 98 8 256 128 9 512 162
| Specification |
|---|
| ECMAScript 2027 LanguageSpecification # sec-array-objects |
Array ObjectThis page was last modified on 2026526 by MDN contributors.
Arrayat()Array.prototype.concat()Array.prototype.copyWithin()Array.prototype.entries()every()Array.prototype.fill()Array.prototype.filter()Array.prototype.find()Array.prototype.findIndex()findLast()findLastIndex()Array.prototype.flat()flatMap()forEach()Array.prototype.includes()Array.prototype.indexOf()Array.prototype.join()Array.prototype.keys()Array.prototype.lastIndexOf()Array.prototype.map()Array.prototype.pop()Array.prototype.push()Array.prototype.reduce()reduceRight()Array.prototype.reverse()Array.prototype.shift()Array.prototype.slice()some()Array.prototype.sort()Array.prototype.splice()toLocaleString()toReversed()toSorted()toSpliced()Array.prototype.toString()Array.prototype.unshift()Array.prototype.values()with()Array.prototype[Symbol.iterator]()Object/FunctionYour blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |