blob: 0e60e242e8ad52e4c15b29bdb97a6439bb103c69 (
plain)
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
|
---
title: >-
SyntaxError: a declaration in the head of a for-of loop can't have an
initializer
slug: Web/JavaScript/Reference/Errors/Invalid_for-of_initializer
tags:
- For문 초기화
- 구문 에러
- 반복문
- 자바스크립트
translation_of: Web/JavaScript/Reference/Errors/Invalid_for-of_initializer
---
<div>{{jsSidebar("Errors")}}</div>
<h2 id="메세지">메세지</h2>
<pre class="syntaxbox">SyntaxError: for-of loop head declarations cannot have an initializer (Edge)
SyntaxError: a declaration in the head of a for-of loop can't have an initializer (Firefox)
SyntaxError: for-of loop variable declaration may not have an initializer. (Chrome)
</pre>
<h2 id="에러_타입">에러 타입</h2>
<p>{{jsxref("SyntaxError")}}</p>
<h2 id="무엇이_잘못되었을까">무엇이 잘못되었을까?</h2>
<p><a href="/en-US/docs/Web/JavaScript/Reference/Statements/for...of">for...of</a> 반복문의 식이 초기화 구문을 포함한 것이 문제입니다. 즉, |<code>for (var i = 0 of iterable)</code>| 구문을 통해 변수가 정의되고 값이 할당된 것을 말합니다. 이 구문은 for-of 반복문에서 허용되지 않습니다. 이 경우 초기화를 할 수 있는 for 반복문이 필요합니다.</p>
<h2 id="예제">예제</h2>
<h3 id="잘못된_for-of_반복문">잘못된 for-of 반복문</h3>
<pre class="brush: js example-bad">let iterable = [10, 20, 30];
for (let value = 50 of iterable) {
console.log(value);
}
// SyntaxError: a declaration in the head of a for-of loop can't
// have an initializer</pre>
<h3 id="올바른_for-of_반복문">올바른 <code>for-of</code> 반복문</h3>
<p>for-of 반복문에서 초기화 구문(<code>value = 50</code>)을 삭제해야 합니다. 50을 더하고 싶다면 다음 예제와 같이 반복문 안에 추가할 수 있습니다.</p>
<pre class="brush: js example-good">let iterable = [10, 20, 30];
for (let value of iterable) {
value += 50;
console.log(value);
}
// 60
// 70
// 80
</pre>
<h2 id="같이_보기">같이 보기</h2>
<ul>
<li><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/for...of">for...of</a></code></li>
<li><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/for...in">for...in</a></code> – 엄격(Strict) 모드에서는 마찬가지로 초기화를 허용하지 않습니다. (<a href="/en-US/docs/Web/JavaScript/Reference/Errors/Invalid_for-in_initializer">SyntaxError: for-in loop head declarations may not have initializers</a>)</li>
<li><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/for">for</a></code> – 초기화를 허용합니다.</li>
</ul>
|