Funnily enough, most of these are not used in the language.
They cannot be used to name variables or parameters.
In saying that,
I did some testing below and that statement’s not entirely accurate.
Usage of keywords in red should be avoided.
Reserved | Keyword | Comments |
abstract | no | |
boolean | no | |
break | yes | |
byte | no | No type of byte in JavaScript |
case | yes | |
catch | yes | |
char | no | JavaScript doesn’t have char. Use string instead |
class | no | technically JavaScript doesn’t have class |
const | no | no const, but read-only can be implemented |
continue | yes | |
debugger | yes | |
default | yes | |
delete | yes | |
do | yes | |
double | no | JavaScript only has number (64 bit floating point) |
else | yes | |
enum | no | |
export | no | |
extends | no | |
false | yes | |
final | no | |
finally | yes | |
float | no | JavaScript only has number (64 bit floating point) |
for | yes | |
function | yes | |
goto | no | |
if | yes | |
implements | no | JavaScript uses prototypal inheritance. Reserved in strict mode |
import | no | |
in | yes | |
instanceof | yes | |
int | no | JavaScript only has number (64 bit floating point) |
interface | no | technically no interfaces, but they can be implemented. Reserved in strict mode |
let | no | Reserved in strict mode |
long | no | JavaScript only has number (64 bit floating point) |
native | no | |
new | yes | use in moderation. See comments in Responses below |
null | yes | |
package | no | Reserved in strict mode |
private | no | access is inferred. Reserved in strict mode |
protected | no | JavaScript has privileged, but it’s inferred. Reserved in strict mode |
public | no | access is inferred. Reserved in strict mode |
return | yes | |
short | no | JavaScript only has number (64 bit floating point) |
static | no | Reserved in strict mode |
super | no | |
switch | yes | |
synchronized | no | |
this | yes | |
throw | yes | |
throws | no | |
transient | no | |
true | yes | |
try | yes | |
typeof | yes | |
var | yes | |
volatile | no | |
void | yes | |
while | yes | |
with | yes | |
yeild | no | Reserved in strict mode |
–
When reserved words are used as keys in object literals,
they must be quoted.
They cannot be used with the dot notation,
so it is sometimes necessary to use the bracket notation instead.
Or better, just don’t use them for your names.
var method; // ok var class; // illegal object = {box: value}; // ok object = {case: value}; // illegal object = {'case': value}; // ok object.box = value; // ok object.case = value; // illegal object['case'] = value; // ok
–
I noticed in Doug Crockfords JavaScript The Good Parts
in Chapter 2 under Names, where he talks about reserved words.
It says:
“It is not permitted to name a variable or parameter with a reserved
word.
Worse, it is not permitted to use a reserved word as the name of an object
property in an object literal or following a dot in a refinement.”
I tested this in Chrome and FireFox with the following results.
var private = 'Oh yuk'; // if strict mode is on: Uncaught SyntaxError: Unexpected strict mode reserved word var break = 'break me'; // Uncaught SyntaxError: Unexpected token break
var myFunc = function (private, break) { // if strict mode is on or off: Uncaught SyntaxError: Unexpected token break // strangly enough, private is always fine as a parameter. }
var myObj = { private: 'dung', // no problem break: 'beetle' // no problem } console.log('myObj.private: ' + myObj.private) // myObj.private: dung console.log(' myObj.break: ' + myObj.break); // myObj.break: beetle
JavaScript also predefines a number of global variables and functions
that you should also avoid using their names for your own variables and functions.
Here’s a list:
- arguments
- Array
- Boolean
- Date
- decodeURI
- decodeURIComponent
- encodeURI
- encodeURIComponent
- Error
- eval
- EvalError
- Function
- Infinity
- isFinite
- isNaN
- JSON
- Math
- NaN
- Number
- Object
- parseFloat
- parseInt
- RangeError
- ReferenceError
- RegExp
- String
- SyntaxError
- TypeError
- undefined
- URIError