JavaScript - package is a reserved keyword
According to MDN, package
is in the "reserved for the future" category. Depending on which version of which browser you are using and whether your code is in strict mode you may or may not be able to use those words as identifiers. In other words, you should avoid them to be safe.
You can safely use reserved words as property names if you use this syntax:
inBlock["package"] = something;
But that doesn't help you with your package
variable. Can you rename it?
You're right, package
is a reserved word in JavaScript (but only in strict mode, which will be why the code works in some places).
package
is future-reserved, which means it's not used for anything, but you can't use it to name variables. However (if you really must), you can use it to name keys in objects like this:
inBlock['package'] = name; // this is ok
As long as you use a string. You can't do this:
inBlock.package = name; // this is not ok
I would say you're better off naming it something else.
For those wondering if this is still true today - package
was added to the future-reserved list in ES-3 (1999), and has remained there until today. At the time of writing, we are at ES-11 (2020), where it is still unavailable.
The relevant parts of the ES-11 2020 spec are:
11.6.2 Note 2:
enum
is not currently used as a keyword in this specification. It is a future reserved word, set aside for use as a keyword in future language extensions.Similarly,
implements
,interface
,package
,private
,protected
, andpublic
are future reserved words in strict mode code.
and 12.1.1 SS: Early Errors:
Identifier
:IdentifierName
but notReservedWord
It is a Syntax Error if this phrase is contained in strict mode code and the StringValue of IdentifierName is: "
implements
", "interface
", "let
", "package
", "private
", "protected
", "public
", "static
", or "yield
".