Reserved Keywords in JavaScript

Reserved keywords in JavaScript

We should be linking to the actual sources of info, rather than just the top google hit.

http://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Reserved_Words

JScript 8.0:
http://msdn.microsoft.com/en-us/library/ttyab5c8.aspx

In Javascript, what does the reserved keyword short do?

They belong to the so-called 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.

short as a reserved word was part of an old ECMAScript specification:

The following are reserved as future keywords by older ECMAScript specifications (ECMAScript 1 till 3)

[...] short [...]

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Keywords

In ECMAScript 5/6 short was removed from the list of reserved words. Nonetheless for compatibility with older browsers not implementing either, you shouldn't use it anyway.

Some JavaScript reserved words function as variables

Reserved keywords as of ECMAScript 6

break case class catch const continue debugger default delete do else
export extends finally for function if import in instanceof new return
super switch this throw try typeof var void while with yield

and abstract and native (more here) were reserved as future keywords by older ECMAScript specifications (ECMAScript 1 till 3).

always reserved : enum

reserved when they are found in strict mode code:

implements package  protected  static  let  interface  private  public

reserved when they are found in module code: await

Should arguments be in the reserved words list for identifiers?

There are several keyword-like things (or even actual keywords) that aren't reserved words in JavaScript (and thus TypeScript); details here. arguments isn't a keyword, it's an implicitly-declared binding (like a variable) within traditional functions (ones using the function keyword) and methods (declared with method syntax in classes or object literals). That means you can shadow it, declare it in a context that doesn't already have it, etc.

I don't see how "arguments" could be used as an identifier.

It can be, but only in loose mode. Here are some examples:

// Arguments isn't implicitly declared outside traditional// functions and methodsconst arguments = 1;console.log(arguments);     // 1try {    arguments = 1.5;} catch (e) {    console.log(e.message); // "Assignment to constant variable." (or similar)}
const fn = () => { // Arrow functions don't have an `arguments` identifier declared for them let arguments = 2; console.log(arguments); // 2};
fn();
function fn2() { if (true) { // Shadows the function-wide `arguments` only within this block let arguments = 3; console.log(arguments); // 3 } // This uses the implicit `arguments` for the function console.log(arguments.length); // 0}
fn2();

Javascript reserved word and object

Instead of using a JS object, you could use the built-in Map type which uses strings/symbols as keys and does not conflict with any existing properties.

Replace
var dictionary = {} with var dictionary = new Map()

Determine if a word is a reserved Javascript identifier

One option would be to do:

var reservedWord = false;
try {
eval('var ' + wordToCheck + ' = 1');
} catch {
reservedWord = true;
}

The only issue will be that this will give false positive for words that are invalid variable names but not reserved words.

As pointed out in the comments, this could be a security risk.

Why enum is a reserved keyword in vanilla javascript?

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.



Related Topics



Leave a reply



Submit