2

Given this definition from MDN:

The TypeError object represents an error when a value is not of the expected type.

Consider this example:

const primitiveTest = 1;
primitiveTest = 2;

TypeError: invalid assignment to const "x" (Firefox)
TypeError: Assignment to constant variable. (Chrome)
TypeError: Assignment to const (Edge)
TypeError: Redeclaration of const 'x' (IE)

But why the TypeError when trying to change primitive values? Why not maybe a syntax error or some other kind of error. What is the logic behind the choice for a TypeError in this case?

Dennis C
  • 24,511
  • 12
  • 71
  • 99
Tiberiu
  • 418
  • 1
  • 3
  • 12
  • 1
    Take a look here: https://stackoverflow.com/a/23436563/4091337 – Teun van der Wijst Nov 28 '18 at 12:02
  • const is constant. it means if you define it you can not change it. try using let instead. – cenk ebret Nov 28 '18 at 12:03
  • I think you answered your question when you asked it. As you said, there is 'binding contract' betweem the name of the constant and the object at a superficial level, and the values inside are freee to change. However, in your const variable, there is no such thing happening, you are trying directly to change ot, which causes an error. – squeekyDave Nov 28 '18 at 12:06

0 Answers0