Welcome to TheCodingCollege.com! In JavaScript, reserved words are terms that the language has predefined for specific purposes, such as keywords, future use, or global objects. Understanding these reserved words is essential to avoid syntax errors and write clean, functional code.
In this guide, we’ll cover:
- What reserved words are in JavaScript.
- Lists of reserved words for ES5 and ES6.
- Best practices for avoiding conflicts in your code.
What Are Reserved Words in JavaScript?
Reserved words in JavaScript are terms that you cannot use as:
- Variable names
- Function names
- Object property names (though technically allowed, it’s discouraged for clarity)
These words are either:
- JavaScript Keywords: Used by the language to define actions, conditions, or structures.
- Future Reserved Words: Reserved for potential future JavaScript enhancements.
- Global Objects: Built-in objects that are always available, such as
Math
orDate
.
List of JavaScript Reserved Words
1. JavaScript Keywords
These are part of the JavaScript language and serve specific functions.
Reserved Word | Purpose |
---|---|
break | Terminates a loop or switch statement. |
case | Defines cases in a switch statement. |
catch | Handles exceptions in try blocks. |
class | Declares a class (ES6). |
const | Declares a constant variable. |
continue | Skips to the next iteration of a loop. |
debugger | Pauses execution for debugging. |
default | Defines the default case in switch . |
delete | Deletes an object property. |
do | Starts a do...while loop. |
else | Defines the alternative block of an if statement. |
export | Exports a module (ES6). |
extends | Extends a class (ES6). |
finally | Defines cleanup code after try...catch . |
for | Declares a for loop. |
function | Declares a function. |
if | Starts a conditional statement. |
import | Imports a module (ES6). |
in | Checks if a property exists in an object. |
instanceof | Checks if an object is an instance of a class. |
let | Declares a block-scoped variable. |
new | Creates an instance of an object. |
return | Returns a value from a function. |
super | Refers to the parent class (ES6). |
switch | Starts a switch statement. |
this | Refers to the current object. |
throw | Throws an exception. |
try | Defines a block of code to test for errors. |
typeof | Returns the data type of a variable. |
var | Declares a variable (function-scoped). |
void | Evaluates an expression without returning a value. |
while | Starts a while loop. |
with | Extends the scope chain (deprecated). |
yield | Pauses and resumes a generator function (ES6). |
2. Future Reserved Words
These are reserved for potential future use in JavaScript and should be avoided as variable names.
Reserved Word | Notes |
---|---|
enum | Reserved for enums. |
await | Reserved for async operations. |
3. Global Objects
While not technically “reserved,” these are part of the JavaScript runtime and shouldn’t be overwritten.
Global Object | Purpose |
---|---|
Array | Represents an array. |
Date | Represents date and time. |
Math | Provides mathematical functions. |
JSON | Handles JSON data. |
Promise | Represents asynchronous operations. |
Best Practices to Avoid Reserved Word Conflicts
- Use Descriptive Names
Always use meaningful variable and function names to avoid conflicts.// Avoid let class = 'Math'; // Error // Use let className = 'Math';
- Follow Naming Conventions
Use camelCase for variables and functions, and PascalCase for classes. - Lint Your Code
Use tools like ESLint to detect potential issues with reserved words. - Keep Updated
Stay informed about new ECMAScript features to avoid using newly reserved words.
Why Learn About Reserved Words with TheCodingCollege.com?
At TheCodingCollege.com, we aim to provide:
- Clear explanations of JavaScript concepts.
- Practical examples to help you avoid errors.
- Tutorials designed to improve your coding skills.
Understanding reserved words ensures your code is error-free and compatible across environments.
Conclusion
Reserved words in JavaScript play a critical role in defining the structure and functionality of the language. Avoiding their misuse ensures that your code is clean, maintainable, and free from syntax errors.