Movatterモバイル変換


[0]ホーム

URL:


  1. 개발자를 위한 웹 기술
  2. JavaScript
  3. JavaScript 참고서
  4. JavaScript error reference
  5. SyntaxError: a declaration in the head of a for-of loop can't have an initializer

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

View in EnglishAlways switch to English

SyntaxError: a declaration in the head of a for-of loop can't have an initializer

메세지

  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)

에러 타입

SyntaxError

무엇이 잘못되었을까?

for...of 반복문의 식이 초기화 구문을 포함한 것이 문제입니다. 즉, |for (var i = 0 of iterable)| 구문을 통해 변수가 정의되고 값이 할당된 것을 말합니다. 이 구문은 for-of 반복문에서 허용되지 않습니다. 이 경우 초기화를 할 수 있는 for 반복문이 필요합니다.

예제

잘못된 for-of 반복문

js
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

올바른for-of 반복문

for-of 반복문에서 초기화 구문(value = 50)을 삭제해야 합니다. 50을 더하고 싶다면 다음 예제와 같이 반복문 안에 추가할 수 있습니다.

js
let iterable = [10, 20, 30];for (let value of iterable) {  value += 50;  console.log(value);}// 60// 70// 80

같이 보기

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp