[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array [Back]  [Original]

Array - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Array

Baseline Widely available *

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 7.

* Some parts of this feature may have varying levels of support.

, Array , .

In this article

JavaScript Array .

  • JavaScript , . ( , .)
  • JavaScript . , ( ) .
  • JavaScript 0 . 0, 1, length 1 .
  • JavaScript . ( JavaScript ).

Array ( ) ( ) . , . , .

toString (, toString() ). .

js
arr.0; //  

JavaScript . (: years[2] years['2']), .

years[2] 2 JavaScript toString . '2' '02' years , true .

js
console.log(years["2"] !== years["02"]);

years['2'] . years['02'] .

length

JavaScript length .

(: join(), slice(), indexOf() ) length .

(: push(), splice() ) length .

js
const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3

JavaScript , length .

js
fruits[5] = "mango";
console.log(fruits[5]); // 'mango'
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 6

length . undefined .

js
fruits.length = 10;
console.log(fruits); // ['banana', 'apple', 'peach', empty x 2, 'mango', empty x 4]
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 10
console.log(fruits[8]); // undefined

length .

js
fruits.length = 2;
console.log(Object.keys(fruits)); // ['0', '1']
console.log(fruits.length); // 2

length .

. , undefined .

, in undefined .

.

undefined .

(mutating method)

, . . , .

, . , .

this.constructor[Symbol.species] .

Array .

.

copyWithin()
fill()
pop() slice(0, -1)
push(v1, v2) concat([v1, v2])
reverse() toReversed()
shift() slice(1)
sort() toSorted()
splice() toSpliced()
unshift(v1, v2) toSpliced(0, 0, v1, v2)

slice() .

js
arr.copyWithin(0, 1, 2); // arr 
const arr2 = arr.slice().copyWithin(0, 1, 2); // arr  
const arr3 = [...arr].copyWithin(0, 1, 2); // arr  

. , . .

js
method(callbackFn, thisArg)

callbackFn .

element

.

index

.

array

.

callbackFn .

callbackFn thisArg ( undefined) this . callbackFn this . callbackFn , this , undefined/null globalThis . this thisArg callbackFn .

, .

.

, every(), find(), findIndex(), findLast(), findLastIndex(), some() callbackFn , .

, (: thisArg ).

sort() , . , thisArg , .

, . length . , .

js
const arrayLike = {
  0: "a",
  1: "b",
  length: 2,
};
console.log(Array.prototype.join.call(arrayLike, "+")); // 'a+b'

length

length 0 253 - 1 . NaN 0 , length undefined 0 .

length . length 253 - 1 TypeError . length 232 - 1 .

js
Array.prototype.flat.call({}); // []

length . length .

js
const a = { length: 0.7 };
Array.prototype.push.call(a);
console.log(a.length); // 0

length . length , 0 length - 1 . ( , .) 0 length - 1 .

DOM (: NodeList HTMLCollection). arguments . .

js
function f() {
  console.log(Array.prototype.join.call(arguments, "+"));
}

f("a", "b"); // 'a+b'

Array()

Array .

Array[@@species]

Array .

Array.from()

Array .

Array.fromAsync()

, , Array .

Array.isArray()

true , false .

Array.of()

Array .

Array.prototype Array .

Array.prototype.constructor

. Array , Array .

Array.prototype[@@unscopables]

ES2015 ECMAScript with .

Array .

length

.

Array.prototype.at()

. .

Array.prototype.concat()

/ .

Array.prototype.copyWithin()

.

Array.prototype.entries()

/ .

Array.prototype.every()

true .

Array.prototype.fill()

.

Array.prototype.filter()

true .

Array.prototype.find()

, undefined .

Array.prototype.findIndex()

, -1 .

Array.prototype.findLast()

, undefined .

Array.prototype.findLastIndex()

, -1 .

Array.prototype.flat()

.

Array.prototype.flatMap()

.

Array.prototype.forEach()

.

Array.prototype.includes()

true false .

Array.prototype.indexOf()

() .

Array.prototype.join()

.

Array.prototype.keys()

.

Array.prototype.lastIndexOf()

( ) , -1 .

Array.prototype.map()

.

Array.prototype.pop()

.

Array.prototype.push()

, length .

Array.prototype.reduce()

( ) "" .

Array.prototype.reduceRight()

( ) "" .

Array.prototype.reverse()

. ( , .)

Array.prototype.shift()

.

Array.prototype.slice()

.

Array.prototype.some()

true .

Array.prototype.sort()

.

Array.prototype.splice()

/ .

Array.prototype.toLocaleString()

. Object.prototype.toLocaleString() .

Array.prototype.toReversed()

, .

Array.prototype.toSorted()

, .

Array.prototype.toSpliced()

, / .

Array.prototype.toString()

. Object.prototype.toString() .

Array.prototype.unshift()

, length .

Array.prototype.values()

.

Array.prototype.with()

, .

Array.prototype[@@iterator]()

values() .

JavaScript .

, Array() , String.prototype.split() .

js
//     'fruits'  .
const fruits = ["Apple", "Banana"];
console.log(fruits.length);
// 2

// Array()   'fruits2'  .
const fruits2 = new Array("Apple", "Banana");
console.log(fruits2.length);
// 2

// String.prototype.split()  'fruits3'  .
const fruits3 = "Apple, Banana".split(", ");
console.log(fruits3.length);
// 2

join() fruits .

js
const fruits = ["Apple", "Banana"];
const fruitsString = fruits.join(", ");
console.log(fruitsString);
// "Apple, Banana"

fruits .

js
const fruits = ["Apple", "Banana"];

//       0.
fruits[0]; // Apple

//       1.
fruits[1]; // Banana

//     
//   1 .
fruits[fruits.length - 1]; // Banana

//     
// undefined .
fruits[99]; // undefined

indexOf() fruits "Banana" () .

js
const fruits = ["Apple", "Banana"];
console.log(fruits.indexOf("Banana"));
// 1

fruits "Banana" "Cherry" . includes() , indexOf() -1 .

js
const fruits = ["Apple", "Banana"];

fruits.includes("Banana"); // true
fruits.includes("Cherry"); // false

// indexOf() -1       .
fruits.indexOf("Banana") !== -1; // true
fruits.indexOf("Cherry") !== -1; // false

push() fruits .

js
const fruits = ["Apple", "Banana"];
const newLength = fruits.push("Orange");
console.log(fruits);
// ["Apple", "Banana", "Orange"]
console.log(newLength);
// 3

pop() fruits .

js
const fruits = ["Apple", "Banana", "Orange"];
const removedItem = fruits.pop();
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItem);
// Orange

: pop() . .

splice() fruits 3 .

js
const fruits = ["Apple", "Banana", "Strawberry", "Mango", "Cherry"];
const start = -3;
const removedItems = fruits.splice(start);
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItems);
// ["Strawberry", "Mango", "Cherry"]

N

splice() fruits .

js
const fruits = ["Apple", "Banana", "Strawberry", "Mango", "Cherry"];
const start = 2;
const removedItems = fruits.splice(start);
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItems);
// ["Strawberry", "Mango", "Cherry"]

shift() fruits .

js
const fruits = ["Apple", "Banana"];
const removedItem = fruits.shift();
console.log(fruits);
// ["Banana"]
console.log(removedItem);
// Apple

: shift() . .

splice() fruits 3 .

js
const fruits = ["Apple", "Strawberry", "Cherry", "Banana", "Mango"];
const start = 0;
const deleteCount = 3;
const removedItems = fruits.splice(start, deleteCount);
console.log(fruits);
// ["Banana", "Mango"]
console.log(removedItems);
// ["Apple", "Strawberry", "Cherry"]

unshift() fruits 0 .

js
const fruits = ["Banana", "Mango"];
const newLength = fruits.unshift("Strawberry");
console.log(fruits);
// ["Strawberry", "Banana", "Mango"]
console.log(newLength);
// 3

splice() "Banana" fruits "Banana" .

js
const fruits = ["Strawberry", "Banana", "Mango"];
const start = fruits.indexOf("Banana");
const deleteCount = 1;
const removedItems = fruits.splice(start, deleteCount);
console.log(fruits);
// ["Strawberry", "Mango"]
console.log(removedItems);
// ["Banana"]

splice() fruits "Banana" "Strawberry" , "Banana" .

js
const fruits = ["Apple", "Banana", "Strawberry", "Mango"];
const start = 1;
const deleteCount = 2;
const removedItems = fruits.splice(start, deleteCount);
console.log(fruits);
// ["Apple", "Mango"]
console.log(removedItems);
// ["Banana", "Strawberry"]

splice() fruits 2 .

js
const fruits = ["Apple", "Banana", "Strawberry"];
const start = -2;
const deleteCount = 2;
const removedItems = fruits.splice(start, deleteCount, "Mango", "Cherry");
console.log(fruits);
// ["Apple", "Mango", "Cherry"]
console.log(removedItems);
// ["Banana", "Strawberry"]

for...of fruits .

js
const fruits = ["Apple", "Mango", "Cherry"];
for (const fruit of fruits) {
  console.log(fruit);
}
// Apple
// Mango
// Cherry

for...of . , every(), filter(), flatMap(), map(), reduce(), reduceRight() , forEach() .

forEach() fruits . .

js
const fruits = ["Apple", "Mango", "Cherry"];
fruits.forEach((item, index, array) => {
  console.log(item, index);
});
// Apple 0
// Mango 1
// Cherry 2

concat() fruits moreFruits combinedFruits . fruits moreFruits .

js
const fruits = ["Apple", "Banana", "Strawberry"];
const moreFruits = ["Mango", "Cherry"];
const combinedFruits = fruits.concat(moreFruits);
console.log(combinedFruits);
// ["Apple", "Banana", "Strawberry", "Mango", "Cherry"]

// 'fruits'   .
console.log(fruits);
// ["Apple", "Banana", "Strawberry"]

// 'moreFruits'   .
console.log(moreFruits);
// ["Mango", "Cherry"]

fruits . , from() , slice() .

js
const fruits = ["Strawberry", "Mango"];

//     .
const fruitsCopy = [...fruits];
// ["Strawberry", "Mango"]

// from()    .
const fruitsCopy2 = Array.from(fruits);
// ["Strawberry", "Mango"]

// slice()    .
const fruitsCopy3 = fruits.slice();
// ["Strawberry", "Mango"]

( , Array.from(), Array.prototype.slice(), Array.prototype.concat()) . JSON.stringify() JSON , JSON.parse() .

js
const fruitsDeepCopy = JSON.parse(JSON.stringify(fruits));

structuredClone() , .

, . , ( ). , .

js
const fruits = ["Strawberry", "Mango"];
const fruitsAlias = fruits;
// 'fruits' 'fruitsAlias'  ,  .
fruits === fruitsAlias; // true
// 'fruits'      'fruitsAlias' .
fruits.unshift("Apple", "Banana");
console.log(fruits);
// ['Apple', 'Banana', 'Strawberry', 'Mango']
console.log(fruitsAlias);
// ['Apple', 'Banana', 'Strawberry', 'Mango']

2

2 . 'p' board[6][4] board[4][4] . [6][4] .

js
const 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`);

//   2  
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

js
const values = [];
for (let 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

RegExp JavaScript . RegExp.prototype.exec() String.prototype.match() .

.

js
//  d,   b,  d .
//  b  d .
//  .

const myRe = /d(b+)(d)/i;
const execResult = myRe.exec("cdbBdbsbz");

console.log(execResult.input); // 'cdbBdbsbz'
console.log(execResult.index); // 1
console.log(execResult); // [ "dbBd", "bB", "d" ]

RegExp.prototype.exec() String.prototype.match() .

Specification
ECMAScript 2027 LanguageSpecification
# sec-array-objects


Web Proxy Viewer  |  New URL  |  Original Page