Reserved keywords in JavaScript

JavascriptReserved Words

Javascript Problem Overview


What JavaScript keywords (function names, variables, etc) are reserved?

Javascript Solutions


Solution 1 - Javascript

Here is my poem, which includes all of the reserved keywords in JavaScript, and is dedicated to those who remain honest in the moment, and not just try to score:

Let this long package float, 
Goto private class if short.
While protected with debugger case,  
Continue volatile interface.
Instanceof super synchronized throw, 
Extends final export throws.  

Try import double enum?  
- False, boolean, abstract function, 
Implements typeof transient break!
Void static, default do,  
Switch int native new. 
Else, delete null public var 
In return for const, true, char
…Finally catch byte.

Solution 2 - 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

Solution 3 - Javascript

To supplement benc's answer, see Standard ECMA-262. These are the official reserved words, but only a pedant ignores the implementation to respect the standard. For the reserved words of the most popular implementations, that is firefox and internet explorer, see benc's answer.

The reserved words in EMCAScript-262 are the Keywords, Future Reserved Words, NullLiteral, and BooleanLiterals, where the Keywords are

break     do        instanceof  typeof
case      else      new         var
catch     finally   return      void
continue  for       switch      while
debugger  function  this        with
default   if        throw
delete    in        try

the Future Reserved Word​s are

abstract  export      interface  static
boolean   extends     long       super
byte      final       native     synchronized
char      float       package    throws
class     goto        private    transient
const     implements  protected  volatile
double    import      public 
enum      int         short

the NullLiteral is

null

and the BooleanLiterals are

true
false

Solution 4 - Javascript

I was just reading about this in JavaScript & jQuery: The Missing Manual:

> Not all of these reserved words will cause problems in all browsers, but it’s best to steer clear of these names when naming variables. > > JavaScript keywords: break, case, catch, continue, debugger, default, delete, do, else, false, finally, for, function, if, in, instanceof, new, null, return, switch, this, throw, true, try, typeof, var, void, while, with. > > Reserved for future use: abstract, boolean, byte, char, class, const, double, enum, export, extends, final, float, goto, implements, import, int, interface, let, long, native, package, private, protected, public, short, static, super, synchronized, throws, transient, volatile, yield. > > Pre-defined global variables in the browser: alert, blur, closed, document, focus, frames, history, innerHeight, innerWidth, length, location, navigator, open, outerHeight, outerWidth, parent, screen, screenX, screenY, statusbar, window.

Solution 5 - Javascript

Here is a browser and language version agnostic way to determine if a particular string is treated as a keyword by the JavaScript engine. Credits to this answer which provides the core of the solution.

function isReservedKeyword(wordToCheck) {
    var reservedWord = false;
    if (/^[a-z]+$/.test(wordToCheck)) {
        try {
            eval('var ' + wordToCheck + ' = 1');
        } catch (error) {
            reservedWord = true;
        }
    }
    return reservedWord;
}

Solution 6 - Javascript

None of the current answers warn that regardless of ES-Dialect, browsers tend to have their own lists of reserved keywords, methods etc on top of what ES dictates.

For example, IE9 prohibits use of logical names like: addFilter, removeFilter (they, among others, are reserved methods).

See http://www.jabcreations.com/blog/internet-explorer-9 for a more extensive 'currently known' list specific to IE9. I have yet find any official reference to them on msdn (or elsewhere).

Solution 7 - Javascript

Here is a list from Eloquent JavaScript book:

  • break
  • case
  • catch
  • class
  • const
  • continue
  • debugger
  • default
  • delete
  • do
  • else
  • enum
  • export
  • extend
  • false
  • finally
  • for
  • function
  • if
  • implements
  • import
  • in
  • instanceof
  • interface
  • let
  • new
  • null
  • package
  • private
  • protected
  • public
  • return
  • static
  • super
  • switch
  • this
  • throw
  • true
  • try
  • typeof
  • var
  • void
  • while
  • with
  • yield

Solution 8 - Javascript

benc's answer is excellent, but for my two cents, I like the w3schools' page on this:

http://www.w3schools.com/js/js_reserved.asp

In addition to listing the keywords reserved by the standard, it also has a long list of keywords you should avoid in certain contexts; for example, not using the name alert when writing code to be run in a browser. It helped me figure out why certain words were highlighting as keywords in my editor even though I knew they weren't keywords.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontitanousView Question on Stackoverflow
Solution 1 - Javascriptart4theSouldView Answer on Stackoverflow
Solution 2 - JavascriptbencView Answer on Stackoverflow
Solution 3 - JavascriptJoseph HolstenView Answer on Stackoverflow
Solution 4 - Javascriptits_meView Answer on Stackoverflow
Solution 5 - JavascriptGOTO 0View Answer on Stackoverflow
Solution 6 - JavascriptGitaarLABView Answer on Stackoverflow
Solution 7 - JavascriptstarhopperxView Answer on Stackoverflow
Solution 8 - JavascriptReikimView Answer on Stackoverflow