JavaScript Editor JavaScript Debugger     JavaScript Editor 



Team LiB
Previous Section Next Section

Appendix C: JavaScript Reserved Words

All languages, including JavaScript, have numerous reserved words that cannot be used as variable names, function names, or any other form of identifiers without causing some problem. If one of these reserved words is used as a user-defined identifier, such as a variable or function name, it should result in a syntax error. For example,

var for="not allowed";
document.write("Variable = " +for);

declares a variable called for, which is, as you have seen, a JavaScript keyword used for looping. You might expect some form of error to occur, and older browsers will throw an error such as the one shown here from Navigator 3,

Click To expand

which make sense. However, newer browsers may not show the expected error. Notice what Internet Explorer displays for the same code.

Click To expand

Sometimes you may find that when a reserved word is used, the code is simply ignored or an error is not shown. For example, use a value of goto instead of for in the previous example and it should work in many browsers, including Internet Explorer.

Generally speaking, reserved words are reserved from use because they already have a defined meaning in some variant of JavaScript or a related technology. Reserved words generally are categorized in three types:

Table C-1 lists the words in the first two categories based upon the JavaScript 1.5 specification combined with Microsoft’s Jscript documentation.

Table C-1: Reserved Words in JavaScript 1.5

>abstract

else

instanceof

switch

>boolean

enum

int

synchronized

>break

export

interface

this

byte

extends

long

throw

case

false

native

throws

catch

final

new

transient

char

finally

null

true

class

float

package

try

const

for

private

typeof

continue

function

protected

val

debugger

goto

public

var

default

if

return

void

delete

implements

short

volatile

do

import

static

while

double

in

super

with

Note 

Some reserved words related to types not found in JavaScript, like byte, are reserved in some versions of ECMAScript and not others.

Beyond these well-known reserved words, there are other words that may have problems under some versions of JavaScript including ECMAScript 4, Jscript.NET, and JavaScript 2.0. While the words shown in Table C-2 may not actually be reserved in your browser, they should be avoided just to be safe.

Table C-2: Potentially Reserved Words

As

event

Is

uint

Assert

get

Namespace

ulong

Decimal

include

Require

use

Ensure

internal

Sbyte

ushort

Exclude

invariant

Set

 

The third category of dangerous identifiers includes names of intrinsic JavaScript objects, functions, and data types. Words like String, parseInt, document, and so on, are included in this category. There are far too many of these “dangerous” identifier names to list, but consider anything in Appendix A or Appendix B to be a JavaScript identifier and inappropriate for other use.

Tip 

Future versions of JavaScript will certainly add more support for object-oriented programming principles as well as increase support for interaction with HTML, XML, and CSS. Therefore, JavaScript programmers should avoid any words specific to these languages, such as “head,” “body,” “frame,” and so on. While many of these words might be safely used, less generic identifiers ought to be used instead, both to future-proof code and to avoid bad programming style.


Team LiB
Previous Section Next Section


JavaScript Editor JavaScript Debugger     JavaScript Editor


©