Matt Snider JavaScript Resource

Understanding JavaScript and Frameworks

Saturday, May 24, 2008

Reserved Words in JavaScript

I ran into an issue this week while running some old code, where I was using a reserved word in JavaScript, or more accurately a reserved word in a specific browser. Almost two years ago, I thought I was rather smart using ’self’ as the variable name to store the Function scope for the Module Pattern, however, several browsers use this internally and can cause surprising conflicts. Later I learned from Douglas Crockford that I should be using ‘that’ as my keyword, because it is yet to be reserved by the EMCAScript standard, any browsers, or server-side JavaScript technologies. However, it did take me a while to realize my mistake, and I felt it about time to revisit the reserved word list. This post is the amalgamation of what I have learned.

First there are the words that are strictly reserved by the current EMCAScript specification. The following are reserved words and may not be used as variables, functions, methods, or object identifiers:

* These links are provided by the Mozilla Developer Center.

Once we taken care of the existing keywords, we want our code to be forward thinking, so we must look out for planned reserved words as well. The following are reserved as possible future keywords by the ECMAScript specification:

* These links are from About.com.

Next we consider JavaScript 2.0 and additional keywords that are in that specification. The following is a list of keywords for JavaScript 2.0 that have not been previously mentioned:

  • as
  • const
  • export
  • import
  • is
  • use

Mozilla has already reserved the following three JavaScript 2.0 words:

* These links are provided by the Mozilla Developer Center.

Next is the large list of predefined Classes and Objects found in all or most browsers:

* These links are from About.com.

And if that wasn’t enough, the following is a large and growing list of global properties and methods:

Global Properties

Global Method

* These links are from About.com.

The Window Object

The Window Object is the global namespace and can be referenced directly within any scope. Also, any variable that is not scoped with the ‘var’ keyword will be attached to the Window Object.
Properties

* These links are from About.com.

Methods

* These links are from About.com.

Events

* These links are from About.com.

And if all that wasn’t enough to worry about, the following are collection of keywords that are reserved by or used by certain browsers:

  • all
  • assign
  • clientInformation
  • element
  • embed
  • embeds
  • event
  • frameRate
  • getClass
  • java
  • JavaArray
  • JavaClass
  • JavaObject
  • JavaPackage
  • layer
  • layers
  • netscape
  • offscreenBuffering
  • opener
  • packages
  • secure
  • status
  • sun
  • taint
  • toSource
  • toString
  • untaint
  • valueOf

Although this is probably not everything, this is the most complete list I have seen of reserved words for JavaScript. I have collected these words here, so each of you can reference it when creating future projects and try to avoid using these words when writing JavaScript.

posted by Matt Snider at 10:12 pm  

4 Comments »

  1. I think you need to make a distinction between the groups you mention here. Keywords and reserved words, as defined in ECMA-262, will cause an error when you try to use them as variable names. Everything else you mention are variables names that are already in use and so shouldn’t be used as variables names in the global scope in order to avoid conflicts, but they do not cause an error. Generally, they are fine to use within a function scope as they won’t overwrite anything and browsers won’t complain about it.

    The JS 2.0 keywords and reserved words are sure to cause headaches when people try to port their scripts.

    Still, a very handy list. Thanks for compiling.

    Comment by Nicholas C. Zakas — May 26, 2008 @ 12:22 pm

  2. Good looks! Thanks for pulling this all together.

    It’d be nice if we could get something like YUI Compressor to check your code for these during a build process.

    Comment by Paul Irish — May 27, 2008 @ 6:40 am

  3. It is definitely a mine field in the global namespace. In Perl all nouns that the developer writes are namespaced away with a prefix of $, @, %, & and maybe some more ugly ASCII or optionally with ALLCAPS. This was done to avoid collisions but unfortunately has the feel of a type system. In PHP I think all developer-defined nouns are prefixed with $ which is a lot easier to understand than Perl’s nightmare but still namespaces safely. In JavaScript we could prefix all our variables with $ but the ECMAScript standard has a note that $ was included for machine generated code. I think they should have left that note out. You could always do something like the old MM_variable that Macromedia did. This sort of namespacing is becoming more appealing to me all the time as it is actually closer in performance to namespacing in Perl and Java than using multiple nested namespace objects like YUI does.

    Comment by Peter Michaux — June 13, 2008 @ 7:30 pm

  4. please help me about self and this keywords …. i want to for example get current script id a thing like this:

    alert(”this script id is : ” + this.id);

    result: this script id is : xsx

    please help me help…….please……

    Comment by XSX — July 2, 2008 @ 4:42 am

RSS feed for comments on this post. TrackBack URI

Leave a comment

Powered by WordPress