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 ๋ฐ˜๋ณต๋ฌธ

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์„ ๋”ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ๋‹ค์Œ ์˜ˆ์ œ์™€ ๊ฐ™์ด ๋ฐ˜๋ณต๋ฌธ ์•ˆ์— ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

let iterable = [10, 20, 30];

for (let value of iterable) {
  value += 50;
  console.log(value);
}
// 60
// 70
// 80

๊ฐ™์ด ๋ณด๊ธฐ