1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
---
title: Array.prototype.push()
slug: Web/JavaScript/Reference/Global_Objects/Array/push
tags:
- Array
- JavaScript
- Method
- Prototype
- Reference
translation_of: Web/JavaScript/Reference/Global_Objects/Array/push
---
<div>{{JSRef}}</div>
<p><code><strong>push()</strong></code> 메서드는 배열의 끝에 하나 이상의 요소를 추가하고, 배열의 새로운 길이를 반환합니다.</p>
<p>{{EmbedInteractiveExample("pages/js/array-push.html")}}</p>
<h2 id="구문">구문</h2>
<pre class="syntaxbox"><code><var>arr</var>.push(<var>element1[</var>, ...[, <var>elementN]]</var>)</code></pre>
<h3 id="매개변수">매개변수</h3>
<dl>
<dt><code>element<em>N</em></code></dt>
<dd>배열의 끝에 추가할 요소.</dd>
</dl>
<h3 id="반환_값">반환 값</h3>
<p>호출한 배열의 새로운 {{jsxref("Array.length", "length")}} 속성.</p>
<h2 id="설명">설명</h2>
<p><code>push</code> 메서드는 배열 끝에 여러 값을 추가합니다.</p>
<p><code>push</code>는 의도적으로 <a href="https://en.wikipedia.org/wiki/Generic_programming">제네릭</a>합니다. 배열을 닯은 객체에 {{jsxref("Function.call", "call()")}} 또는 {{jsxref("Function.apply", "apply()")}}로 사용될 수 있다. <code>push</code> 메서드는 주어진 값을 입력하는 것을 어디에 시작할 것인지를 결정하기 위해 <code>length</code> 속성에 의존한다. 만약 <code>length</code> 속성이 숫자로 변환 될 수 없다면 인덱스는 0을 사용한다. <code>length</code> 가 생성되게 될 경우에 길이 값이 존재하지 않을 가능성을 포함한다. </p>
<p>String(문자열)이 변경할 수 없는 것처럼 비록 이 명령어의 어플리케이션들이 적합하지 않다고 할지라도 단지 원래 배열 같은 객체는 {{jsxref("Global_Objects/String", "strings", "", 1)}}이다.</p>
<h2 id="예시">예시</h2>
<h3 id="배열에_엘리먼트를_추가_하기">배열에 엘리먼트를 추가 하기</h3>
<p>다음 코드는 두가지 엘리먼트를 포함하는 스포츠 배열을 생성하고 두개의 엘리먼트를 추가 한다. <code>total</code> 변수는 추가한 배열의 새 길이 값을 포함한다. </p>
<pre class="brush: js">var sports = ['축구', '야구'];
var total = sports.push('미식축구', '수영');
console.log(sports); // ['축구', '야구', '미식축구', '수영']
console.log(total); // 4
</pre>
<h3 id="두개의_배열을_합치기">두개의 배열을 합치기</h3>
<p>이 예제는 두번째 배열의 모든 엘리먼트를 push 하기 위해 {{jsxref("Function.apply", "apply()")}}를 사용한다.</p>
<p>만약 두번째 배열( 아래 예제에서는 moreVegs )이 매우 클 경우, 이 메소드를 사용하지 말아야 한다. 실제로 한 함수가 사용가능한 매개변수의 최대 개수에는 제한이 있기 때문이다. 더 자세한 사항은 {{jsxref("Function.apply", "apply()")}} 에서 찾아볼 수 있다.</p>
<pre class="brush: js">var vegetables = ['설탕당근', '감자'];
var moreVegs = ['셀러리', '홍당무'];
// 첫번째 배열에 두번째 배열을 합친다.
// vegetables.push('셀러리', '홍당무'); 하는 것과 동일하다.
Array.prototype.push.apply(vegetables, moreVegs);
console.log(vegetables); // ['설탕당근', '감자', '셀러리', '홍당무']
</pre>
<h2 id="명세">명세</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">명세서</th>
<th scope="col">상태</th>
<th scope="col">비고</th>
</tr>
<tr>
<td>{{SpecName('ES3')}}</td>
<td>{{Spec2('ES3')}}</td>
<td>초기 정의. JavaScript 1.2에서 구현되었음.</td>
</tr>
<tr>
<td>{{SpecName('ES5.1', '#sec-15.4.4.7', 'Array.prototype.push')}}</td>
<td>{{Spec2('ES5.1')}}</td>
<td> </td>
</tr>
<tr>
<td>{{SpecName('ES6', '#sec-array.prototype.push', 'Array.prototype.push')}}</td>
<td>{{Spec2('ES6')}}</td>
<td> </td>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-array.prototype.push', 'Array.prototype.push')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="브라우저_호환성">브라우저 호환성</h2>
<p>{{Compat("javascript.builtins.Array.push")}}</p>
<h2 id="같이_보기">같이 보기</h2>
<ul>
<li>{{jsxref("Array.prototype.pop()")}}</li>
<li>{{jsxref("Array.prototype.shift()")}}</li>
<li>{{jsxref("Array.prototype.unshift()")}}</li>
<li>{{jsxref("Array.prototype.concat()")}}</li>
</ul>
|