I am a C ++ programmer (not very good), and I know what enum is. When I was reading about lexical grammar (Source MDN), I saw a new keyword, enum. I tried it on NodeJS. And it works! (Well yes, but actually no ...).
enum someEnum {
}
And NodeJS throws an error...
SyntaxError: Unexpected reserved word
←[90m at wrapSafe (internal/modules/cjs/loader.js:1060:16)←[39m
←[90m at Module._compile (internal/modules/cjs/loader.js:1108:27)←[39m
←[90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:1164:10)←[39m
←[90m at Module.load (internal/modules/cjs/loader.js:993:32)←[39m
←[90m at Function.Module._load (internal/modules/cjs/loader.js:892:14)←[39m
←[90m at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)←[39m
←[90m at internal/main/run_main_module.js:17:47←[39m
But look! NodeJS considers it a keyword. The question is, is there a right way?
2 Answers 2
As said in the MDN source you linked, enum has been reserved for future use, but the functionality has not been implemented yet. You can either use the enum package, or use something like Typescript that does implement enums.
Comments
If you notice, its section is Future Reserved Keywords, which
They have no special functionality at present, but they might at some future time, so they cannot be used as identifiers.
Currently, enum is supported in JS's superset, Typescript
enumdoesn't do anything currently. It's just a reserved keyword for the futuer.