๐Ÿšจ Assignment to constant variable.

๐Ÿšจ Assignment to constant variable.

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํ€ด์ฆˆ๋ฅผ ํ‘ธ๋Š”๋ฐ ์ฝ”๋“œ์— ๋นจ๊ฐ„ ํ‘œ์‹œ๋„ ์•ˆ๋‚ฌ๋Š”๋ฐ ์—๋Ÿฌ๊ฐ€ ๋–ด๋‹ค.

์ง์—ญํ•˜๋ฉด ์ƒ์ˆ˜๊ฐ’์„ ๋ณ€์ˆ˜์— ํ• ๋‹นํ–ˆ๋‹ค๋Š” ๋œป์ด๋‹ค.

function countBiggerThanTen(numbers) {
  let sum = 0;
  numbers.forEach((number) => {
    if (number > 10) {
      sum += 1; // TypeError: Assignment to constant variable.
    }
  });
  console.log(sum);
}

์›์ธ

์ด๋ฏธ ์„ ์–ธํ•œ const ๋ณ€์ˆ˜์— ์ƒˆ๋กœ์šด ๊ฐ’์„ ํ• ๋‹นํ–ˆ์„ ๋•Œ ๋ฐœ์ƒํ•œ๋‹ค.

์œ„ ์ฝ”๋“œ์—์„œ ๊ณ„์† ๊ฐ’์ด ๋ฐ”๋€Œ๋Š” sum์„ ์ƒ์ˆ˜๋กœ ์„ ์–ธํ–ˆ๋‹ค.

ํ•ด๊ฒฐ

๊ฐ’์„ ์žฌํ• ๋‹น์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” let์œผ๋กœ ๋ฐ”๊พธ์–ด ์ฃผ์—ˆ๋”๋‹ˆ ์ •์ƒ์ ์œผ๋กœ ๋™์ž‘ํ–ˆ๋‹ค.

Leave a comment