loading...

JavaScript – Identifiers and Reserved Words

An identifier is simply a name. In
JavaScript, identifiers are used to name variables and functions and
to provide labels for certain loops in JavaScript code. A JavaScript
identifier must begin with a letter, an underscore ( _), or a dollar sign ( $). Subsequent characters can be letters,
digits, underscores, or dollar signs. (Digits are not allowed as the
first character so that JavaScript can easily distinguish identifiers
from numbers.) These are all legal identifiers:

i
my_variable_name
v13
_dummy
$str

For portability and ease of editing, it is common to use only
ASCII letters and digits in identifiers. Note, however, that
JavaScript allows identifiers to contain letters and digits from the
entire Unicode character set. (Technically, the ECMAScript standard
also allows Unicode characters from the obscure categories Mn, Mc, and
Pc to appear in identifiers after the first character.) This allows
programmers to use variable names from non-English languages and also
to use mathematical symbols:

var sí = true;
var π = 3.14;

Like any language, JavaScript reserves certain identifiers for
use by the language itself. These “reserved words” cannot be used as
regular identifiers. They are listed below.

Reserved Words

JavaScript reserves a number of identifiers as the keywords of
the language itself. You cannot use these words as identifiers in
your programs:

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

JavaScript also reserves certain keywords that are not
currently used by the language but which might be used in future
versions. ECMAScript 5 reserves the following words:

class     const     enum      export    extends   import    super

In addition, the following words, which are legal in ordinary
JavaScript code, are reserved in strict mode:

implements     let            private        public         yield
interface      package        protected      static

Strict mode also imposes restrictions on the use of the
following identifiers. They are not fully reserved, but they are not
allowed as variable, function, or parameter names:

arguments      eval

ECMAScript 3 reserved all the keywords of the Java language,
and although this has been relaxed in ECMAScript 5, you should still
avoid all of these identifiers if you plan to run your code under an
ECMAScript 3 implementation of JavaScript:

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

JavaScript predefines a number of global variables and
functions, and you should avoid using their names for your own
variables and functions:

arguments           encodeURI           Infinity  Number          RegExp
Array               encodeURIComponent  isFinite  Object          String
Boolean             Error               isNaN     parseFloat      SyntaxError
Date                eval                JSON      parseInt        TypeError
decodeURI           EvalError           Math      RangeError      undefined
decodeURIComponent  Function            NaN       ReferenceError  URIError

Keep in mind that JavaScript implementations may define other
global variables and functions, and each specific JavaScript
embedding (client-side, server-side, etc.) will have its own list of
global properties. See the Window object in Part IV
for a list of the global variables and functions defined by
client-side JavaScript.

Comments are closed.

loading...