🚨 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