JavaScript Reserved Words
JavaScript has a set of reserved words that have specific meanings within the language and cannot be used as identifiers for variables, functions, or other elements. Understanding these reserved words is crucial for avoiding errors and ensuring that your code runs smoothly. This article outlines the reserved words in JavaScript and explains their significance.
What Are Reserved Words?
Reserved words in JavaScript are keywords that are used by the language itself to perform specific operations or define functionality. These words are predefined and have special meanings, so they cannot be used as variable names, function names, or identifiers.
For example, using a reserved word like class
as a variable name will result in a syntax error because class
is a keyword used for defining classes in JavaScript.
List of JavaScript Reserved Words
Here is a list of reserved words that you should avoid using as identifiers in your JavaScript code:
Keywords:
Keywords | |||||
---|---|---|---|---|---|
break |
case |
catch |
class |
const |
continue |
debugger |
default |
delete |
do |
else |
export |
extends |
finally |
for |
function |
if |
import |
in |
instanceof |
new |
return |
super |
switch |
throw |
try |
typeof |
var |
void |
while |
with |
yield |
Future Reserved Words:
Future Reserved Words | |||
---|---|---|---|
enum |
implements |
interface |
let |
package |
private |
protected |
public |
static |
Literal Values:
Literal Values | ||
---|---|---|
null |
true |
false |
Examples of Reserved Words in Use
Let’s take a look at a few examples of how reserved words are used and what happens if you try to use them as identifiers.
Valid Usage of Reserved Words
class Person {
constructor(name) {
this.name = name;
}
}
const person = new Person('John');
console.log(person.name); // John
In this example, class
and const
are used as JavaScript keywords to define a class and a constant variable, respectively.
Invalid Usage of Reserved Words
const class = 'Math'; // SyntaxError: Unexpected token 'class'
Here, class
is incorrectly used as a variable name, which will throw a syntax error because class
is a reserved word in JavaScript.
Conclusion
JavaScript reserved words are essential to the language and are reserved for specific functionalities. It’s important to avoid using these words as variable names, function names, or identifiers in your code. Familiarizing yourself with this list of reserved words will help you write error-free and well-structured JavaScript code.
Comments
Post a Comment