SyntaxError: return not in function

๋ฉ”์‹œ์ง€

SyntaxError: return not in function
SyntaxError: yield not in function

์—๋Ÿฌ ํƒ€์ž…

๋ญ๊ฐ€ ์ž˜๋ชป๋œ ๊ฑธ๊นŒ์š”?

return ๋˜๋Š” yield ๋ฌธ์žฅ์ด function ๋ฐ–์—์„œ ํ˜ธ์ถœ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ํ˜น์‹œ ์–ด๋”˜๊ฐ€์— ์ค‘๊ด„ํ˜ธ๋ฅผ ๋น ํŠธ๋ฆฌ์‹  ๊ฑด ์•„๋‹Œ๊ฐ€์š”? return ๊ณผ yield function ๋‚ด์— ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค, ์™œ๋ƒํ•˜๋ฉด ์ด๊ฒƒ๋“ค์€ function์˜ ์‹คํ–‰ ์ข…๋ฃŒ (๋˜๋Š” ์ผ์‹œ์ •์ง€ ๋ฐ ์žฌ๊ฐœ)๋ฅผ ์˜๋ฏธํ•˜๊ณ  function ํ˜ธ์ถœ์ž์—๊ฒŒ ํŠน์ • ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

์˜ˆ์ œ

var cheer = function(score) {
  if (score === 147)
    return 'Maximum!';
  };
  if (score > 100) {
    return 'Century!';
  }
}

// SyntaxError: return not in function

์ฒ˜์Œ ๋ณผ ๋•Œ๋Š” ์ค‘๊ด„ํ˜ธ๊ฐ€ ์ œ๋Œ€๋กœ ์žˆ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ธ๋‹ค, ํ•˜์ง€๋งŒ ์ด ์ฝ”๋“œ ์กฐ๊ฐ์—๋Š” if ๋ฌธ์žฅ ๋‹ค์Œ์— ์ค‘๊ด„ํ˜ธ๊ฐ€ ํ•˜๋‚˜ ๋น ์ ธ ์žˆ๋‹ค. ๋‹ค์Œ์ฒ˜๋Ÿผ ๊ณ ์ณ์•ผ ํ•œ๋‹ค:

var cheer = function(score) {
  if (score === 147) {
    return 'Maximum!';
  }
  if (score > 100) {
    return 'Century!';
  }
};

๋˜ ๋‹ค๋ฅธ ๋‚ด์šฉ