The following table lists JavaScript reserved keywords:
break |
delete |
for |
super |
void |
case |
do |
function |
switch |
while |
catch |
else |
if |
this |
with |
class |
enum |
import |
throw |
|
const |
export |
in |
true |
|
continue |
extends |
new |
try |
|
debugger |
false |
null |
typeof |
|
default
|
finally |
return |
var |
|
You should avoid using reserved words, intrinsic (built-in) JavaScript objects and functions for naming your variables.
Using any of the reserved keywords causes a compilation error when your script is first loaded. Using a name reserved for an intrinsic object or function can cause odd behavior problems if you attempt to use both your variable and the original entity of the same name in the same script. For example, the following script is bound to cause problems:
var String = "This is a string";
document.write(String);
var str = new String("This is another string");
document.write(str);
|
Paste the example above into JavaScript Editor document.
Use the mouse to highlight and select only the first two lines of code and click the Execute button.
What happens? The script runs without problems.
Click inside the document to un-select the first two lines (alternatively, press Ctrl+A to select the entire script), and click the Execute button.
In this case you get the "TypeError: String is not a constructor" message pointing to the third line of code. Because you used the name of an intrinsic object, String, as a variable name you can no longer use the String object in the same script.
In this example it is easy to locate the problem, but if you are working on big script with thousands of lines of code in-between, you might spend quite some time wondering what went wrong with your code.
The built-in (intrinsic) JavaScript objects are:
Array
Boolean
Date
Function
Global
Math
Number
Object, and
String
To avoid problems never use any of the reserved words, built-in objects or functions to name your variables.
Next |