7

I was trying to declare a variable named enum but got Uncaught SyntaxError: Unexpected reserved word error.

I'm not using typescript, so why is enum a reserved keyword?

I was searching for it and realized that enum is a reserved keyword, but protected is also reserved and doesn't give me the error.

enter image description here

I also couldn't find what enum is used for or how it works in vanilla js.

Jacob Nelson
  • 15
  • 1
  • 5
Vencovsky
  • 28,550
  • 17
  • 109
  • 176
  • 6
    Documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Keywords and the text says: *"The following are reserved as future keywords by the ECMAScript specification. They have no special functionality at present, but they might at some future time, so they cannot be used as identifiers."* – epascarello Jan 24 '20 at 18:10
  • 6
    Why not? It's used in many languages, so ECMAScript reserved it for future possible use, so that people don't start using it all over their code, making it impossible for them to use it later. – Heretic Monkey Jan 24 '20 at 18:10
  • 4
    `protected` is only reserved in [strict mode](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode) – some Jan 24 '20 at 18:12
  • @epascarello Thanks, now it makes sense. That is what I needed – Vencovsky Jan 24 '20 at 18:12
  • Note that since Javascript is very case-sensitive, you could still use `Enum` as it is not reserved. – Trashman Apr 07 '22 at 19:47

1 Answers1

10

According to the docs

The following are reserved as future keywords by the ECMAScript specification. They have no special functionality at present, but they might at some future time, so they cannot be used as identifiers.

And enum is always reserved keyword.

Vencovsky
  • 28,550
  • 17
  • 109
  • 176