SyntaxError: missing ; before statement

๋ฉ”์‹œ์ง€

SyntaxError: missing ; before statement

์˜ค๋ฅ˜ ํƒ€์ž…

SyntaxError.

๋ฌด์—‡์ด ์ž˜ ๋ชป ๋˜์—ˆ๋‚˜?

์–ด๋”˜๊ฐ€์— ์„ธ๋ฏธ ์ฝœ๋ก (;)์ด ๋น ์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.  JavaScript ๊ตฌ๋ฌธ์€ ๋ฐ˜๋“œ์‹œ ์„ธ๋ฏธ ์ฝœ๋ก ์œผ๋กœ ๋๋‚˜์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ผ๋ถ€๋Š” ์ž๋™ ์„ธ๋ฏธ์ฝœ๋ก  ์‚ฝ์ž… (automatic semicolon insertion (ASI))์˜ ์˜ํ–ฅ์„ ๋ฐ›์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด ๊ฒฝ์šฐ์—๋Š” ์ง์ ‘ ์„ธ๋ฏธ ์ฝœ๋ก ์„ ์จ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์•ผ JavaScript๊ฐ€ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ํ•ด์„ ๋  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๋‚˜, ๊ฐ€๋”, ์ด ์˜ค๋ฅ˜๋Š” ๋˜ ๋‹ค๋ฅธ ์˜ค๋ฅ˜์˜ ๊ฒฐ๊ณผ๋กœ๋„ ๋‚˜ํƒ€๋‚  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด์„ escape ๋ฌธ์ž๋กœ ์ ์ ˆํžˆ ์ฒ˜๋ฆฌํ•ด ์ฃผ์ง€ ์•Š์•˜์„ ๋•Œ, ๋˜๋Š” var ํ‚ค์›Œ๋“œ๋ฅผ ์ž˜ ๋ชป ์‚ฌ์šฉํ–ˆ์„ ๋•Œ์™€ ๊ฐ™์€ ๊ฒฝ์šฐ ์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ ์–ด๋”˜๊ฐ€์— ๋„ˆ๋ฌด ๋งŽ์€ ๊ด„ํ˜ธ ๊ธฐํ˜ธ๋ฅผ ์‚ฌ์šฉํ–ˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ์—๋Š” ๋ฌธ๋ฒ•์„ ์‹ ์ค‘ํžˆ ๊ฒ€ํ† ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 

์˜ˆ์ œ

Escape ์ฒ˜๋ฆฌ๋˜์ง€ ์•Š์€ ๋ฌธ์ž์—ด

์ด ์˜ค๋ฅ˜๋Š” ๋ฌธ์ž์—ด์„ escape ์ฒ˜๋ฆฌ ํ•˜์ง€ ์•Š์•˜์„ ๋•Œ, JavaScript ์—”์ง„์ด ๋ฌธ์ž์—ด ๋์„ ๋ฏธ๋ฆฌ ์˜ˆ์ธกํ•˜์—ฌ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์ž๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. :

var foo = 'Tom's bar';
// SyntaxError: missing ; before statement

๋ฌธ์ž์—ด์„ ๋ฌถ์–ด์ค„ ๋•Œ ์Œ ๋”ฐ์˜ดํ‘œ๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜, ์—ญ์Šฌ๋ž˜์‹œ๋ฅผ ์ด์šฉํ•ด์„œ ํ™‘๋”ฐ์˜ดํ‘œ๋ฅผ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. :

var foo = "Tom's bar";
var foo = 'Tom\'s bar';

var์™€ ํ•จ๊ป˜ ์„ ์–ธํ•˜๋Š” ๊ฒƒ

์ด๋ฏธ var ์„ ์–ธ๋œ object๋‚˜ array์˜ ์š”์†Œ๋ฅผ ์ƒˆ๋กœ ์„ ์–ธ ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

var obj = {};
var obj.foo = 'hi'; // SyntaxError missing ; before statement

var array = [];
var array[0] = 'there'; // SyntaxError missing ; before statement

๋Œ€์‹ ์—, var  ํ‚ค์›Œ๋“œ๋ฅผ ์ƒ๋žตํ•˜๊ณ  ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. :

var obj = {};
obj.foo = 'hi';

var array = [];
array[0] = 'there';

์ฐธ์กฐ ๋ฌธ์„œ