最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Why enum is a reserved keyword in vanilla javascript? - Stack Overflow

programmeradmin5浏览0评论

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.

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

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.

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

Share Improve this question edited Jan 24, 2020 at 20:06 Jacob Nelson 151 silver badge5 bronze badges asked Jan 24, 2020 at 18:08 VencovskyVencovsky 31.7k22 gold badges130 silver badges197 bronze badges 5
  • 6 Documentation: developer.mozilla/en-US/docs/Web/JavaScript/Reference/… 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 Commented Jan 24, 2020 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 Commented Jan 24, 2020 at 18:10
  • 4 protected is only reserved in strict mode – some Commented Jan 24, 2020 at 18:12
  • @epascarello Thanks, now it makes sense. That is what I needed – Vencovsky Commented Jan 24, 2020 at 18:12
  • Note that since Javascript is very case-sensitive, you could still use Enum as it is not reserved. – Trashman Commented Apr 7, 2022 at 19:47
Add a ment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论