Somebody already asked a very similar question years ago - why is class a reserved word in JavaScript?. The answer was, so that you could add the class
keyword, which we have now.
But I'd like to take that question one step further. Why does having a class
keyword make it neccessary to forbid using class
as an identifier? Is there anything in JS grammar that would make it ambiguous? Or is it so JS can be parsed with a certain kind of parser? I don't see that something like this would be impossible (although silly of course):
class class {
// ...
}
let x = new class();
Please don't answer "because the spec says so" or "a keyword is forbidden as an identifier by definition". Why does the spec have to be this way? Or is it an arbitrary or aestetic descision?
This question is not completely idle curiosity. The fact that class
is a forbidden identifier comes up in the debate whether React should use class
or className
in JSX, for example.