Reserved keywords in JavaScript
Asked Answered
F

8

170

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

Focal answered 25/8, 2008 at 15:29 Comment(1)
mathiasbynens.be/notes/reserved-keywordsUlibarri
S
112

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

Supra answered 25/8, 2008 at 15:29 Comment(0)
T
1513

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.
Tatter answered 24/8, 2012 at 17:34 Comment(3)
How about yield?Eroticism
This poem is a legend! But unfortunately the included words were reserved in ES3 (byte, long, etc.) that are no longer reserved as of ES5, ES6 and next versions.Begat
Updated with few more Reserved keywords: "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, [ yield arguments eval await ] 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."Duff
S
112

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

Supra answered 25/8, 2008 at 15:29 Comment(0)
G
63

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
Gamophyllous answered 29/9, 2008 at 7:7 Comment(6)
Joseph, thanks for adding that info. I found that PDF in google, but didn't have time to open and read it all.Supra
The "abstract" future reserved word is not mentioned in neither the ES5 spec nor the ES6 draft. Where did that come from?Cohlier
Found it! It was present in ES3 as a future reserved word, along with a long list of others, but it was removed in ES5.Cohlier
What kind of answer is this. It doesn't even rhyme.Phaidra
Did the Future Reserved Words change? In the linked PDF I don’t see int or bool etc. in the listing.Leger
I cannot see let in here, but I see it in the docu: ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdfMaguire
B
20

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.

Bushranger answered 20/5, 2013 at 14:27 Comment(4)
Used location in a script and got very odd behaviour, very useful post.Mattah
"Reserved for future use" :: All Java words... quite lazy really.Margueritamarguerite
Note that “reserved” is not the same as “pre-initialized”. In the browser, alert is already initialized, but nothing stops you from reassigning alert = 5. However, you cannot set window to 5, but you can use it as a local variable. That's not possible with reserved keywords, future use, null, false, true.Ulrikaumeko
The more voted answers missed yield by my quick check so +1 from me. These can in ES5.1 be activated by strict mode: implements interface let package private protected public static yieldMichamichael
P
5

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;
}
Pattiepattin answered 18/3, 2015 at 12:25 Comment(2)
If you have to use eval for anything, it most likely means you're doing it wrong.Faustena
This is perfect for a test case run at build time, completely valid as long as it's not something you have exposed at runtime.Unattached
T
3

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).

Tetrafluoroethylene answered 8/5, 2015 at 18:39 Comment(0)
S
2

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
Supersaturate answered 19/11, 2017 at 5:37 Comment(0)
C
0

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.

Conant answered 24/3, 2016 at 5:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.