JavaScript SyntaxError â Unexpected reserved word
A "SyntaxError: Unexpected reserved word" error in JavaScript is when a reserved word is used incorrectly, generally in a different identifier kind of context, this error will not allow the code to run and frequently results from using keywords as variable names, function names or by putting them at suitable positions in the code, in this article we will understand this type of error with examples, what causes it and how one can resolve it.
Understanding an error
An âUnexpected reserved wordâ error happens when JavaScript reserved words (like class, const, function, let etc.) are used where they should not be, using reserved words incorrectly breaks JavaScript syntax rules because those terms have specific meanings and functions within the language.
Case 1: Error Cause: Using Reserved Words as Variable Names
In this case if we attempt to use a reserved word as a variable name will cause an "Unexpected reserved word" error.
Example: In the given below example we are using a reserved keyword as a variable name.
let let = 10;
console.log(let);
Output:
SyntaxError: Unexpected reserved word
Resolution of error
To avoid such error, you should avoid use of a reserved words as variable names, choose a different identifier that is not a reserved word.
let value = 10;
console.log(value);
Output
10
Case 2: Error Cause: Incorrect Use of Reserved Words in Expressions
In this case if we attempt to use a reserved word incorrectly within expressions or statements can trigger an Unexpected reserved word error.
Example: In the given below example we are using a reserved keyword as a variable name.
function() {
return class;
}
Output:
SyntaxError: Unexpected reserved word
Resolution of error
â¤To avoid such kind of error you should ensure that the reserved words are used in their appropriate contexts. â¤
function getClass() {
return "class";
}
console.log(getClass());
Output
class
Case 3: Error Cause: Reserved Words as Function Names
In this case if we attempt to use a reserved word as a function name will cause an "Unexpected reserved word" error.
Example: In the given below example we are using a reserved keyword as a variable name.
function delete() {
console.log("Delete function");
}
delete();
Output:
SyntaxError: Unexpected reserved word
Resolution of error
To avoid such kind error you should avoid using reserved words as function names, choose a different identifier that is not a reserved word.
function remove() {
console.log("Remove function");
}
remove();
Output
Remove function
Case 4: Error Cause: Reserved Words in Class Declarations
In this case if we attempt to use a reserved word incorrectly within class declarations can trigger an Unexpected reserved word error.
Example: In the given below example we are using a reserved keyword as a variable name.
class {
constructor() {
this.name = "Example";
}
}
Output:
SyntaxError: Unexpected reserved word
Resolution of error
To avoid such kind of error you should ensure that class declarations are correctly formatted and that reserved words are used in their proper context.
class Example {
constructor() {
this.name = "Example";
}
}
let obj = new Example();
console.log(obj.name);
Output
Example
Conclusion
In JavaScript ensure reserved words are properly used according to the syntax rules set so as to prevent âUnexpected reserved wordâ errors, to do this, avoid using the reserved words as variable or function names in your codes and also, use the reserved words properly in expressions and class declarations for code integrity and its functionality.