🧮 JavaScript Variables & Data Types
Estimated reading: 4 minutes 10 views

🚫 JavaScript Reserved Keywords – Complete Guide with Examples


🧲 Introduction – Why Reserved Keywords Matter in JavaScript

💡 Ever tried naming a variable class or return and got an error? That’s because some words in JavaScript are off-limits.

In JavaScript, reserved keywords are special words that are part of the language syntax. They serve predefined roles in controlling program flow, declaring variables, handling exceptions, and more. Attempting to use these keywords for identifiers like variable or function names can break your code and trigger syntax errors.

🔍 What You’ll Learn:

  • What reserved keywords are in JavaScript
  • Why using them incorrectly causes errors
  • The full categorized list of reserved keywords
  • Best practices for avoiding naming conflicts

📚 Core Concepts and Theory

🔑 What Are Reserved Keywords?

Reserved keywords are reserved by the JavaScript language engine to maintain the structure, rules, and logic of the code. These cannot be redefined or repurposed for custom variable names, function names, or class names.

🔹 They ensure predictable behavior of your code.
🔹 They are case-sensitive in JavaScript (Var is not the same as var).
🔹 Reserved keywords evolve with each ECMAScript version — so staying updated is essential.

✅ Common Use Cases for Keywords

CategoryPurpose
Control FlowDirect how the code executes (e.g. if, while)
DeclarationsCreate variables or functions (e.g. let, const)
Object-Oriented ProgrammingDefine and use classes and objects (e.g. class, this)
Type/Value KeywordsRepresent values and types (e.g. null, typeof)
Contextual (Future-Use)Reserved in strict or async contexts (e.g. await, yield)

💻 Code Examples & Explanation

❌ Attempting to Use a Reserved Keyword as a Variable

var for = 10;
console.log(for);

🔍 Explanation:

  • var for = 10;
    ➤ ❌ This throws a SyntaxError because for is a keyword used for loops.
  • console.log(for);
    ➤ Would print the value, if it were allowed — but the code won’t even compile.

✅ Correct Usage with a Valid Identifier

var itemCount = 10;
console.log(itemCount);

🔍 Explanation:

  • itemCount is a custom identifier, and not a reserved keyword — so it’s valid.
  • Always use meaningful, non-reserved names like itemCount, total, userData, etc.

📋 Complete List of JavaScript Reserved Keywords

🔁 Control Flow & Exception Handling

break, case, catch, continue, default, do, else, finally, 
for, if, return, switch, throw, try, while, with

🛠️ Variable & Function Declaration

const, function, let, var

🧱 Object-Oriented Programming

class, extends, super, this

🎯 Type & Value Keywords

false, null, true, typeof, void

⏳ Contextual or Future-Reserved

These are not always reserved in non-strict mode but must be avoided to ensure future compatibility:

await, enum, implements, import, interface, package, 
private, protected, public, static, yield

📝 Note: await and yield are only reserved inside async or generator functions, respectively.


🚫 Why You Should Never Use Reserved Keywords as Identifiers

Trying to reuse keywords breaks the code’s grammar. For example:

let return = 20; // ❌ SyntaxError: Unexpected token 'return'

📛 This is because return is a keyword used to exit from functions and cannot be redefined.


✅ Best Practices for Naming in JavaScript

🔐 Avoid These Mistakes:

  • Using reserved keywords as variable or function names.
  • Guessing whether a word is reserved — always check the MDN Lexical Grammar Guide.

🔎 Do This Instead:

  • ✅ Use clear, descriptive names like totalAmount, userList, handleSubmit
  • ✅ Follow camelCase for variable names: userData, orderId
  • ✅ Stay updated with ECMAScript releases to avoid newly introduced reserved terms

🧾 Summary & Key Takeaways

🔸 Reserved keywords are integral to JavaScript’s syntax
🔸 Avoid using them for identifiers to prevent errors
🔸 Some keywords are conditionally reserved in strict or async contexts
🔸 Always use meaningful and custom variable names
🔸 Stay informed via MDN or ECMAScript updates


❓ FAQ – JavaScript Reserved Keywords

❓ Can I use await or yield as variable names?

Only outside of async or generator functions. Within those, they are reserved.

❓ Are reserved keywords case-sensitive?

Yes, Var is allowed, but var is reserved.

❓ What happens if I use a reserved keyword as a class name?

You’ll get a SyntaxError. Reserved keywords cannot be used as class names.

❓ Where can I find the most updated list of reserved keywords?

Refer to MDN Web Docs – Lexical Grammar for the most reliable and updated list.

❓ Are all contextual keywords reserved in every JavaScript version?

No. Some are only reserved in strict mode or specific ES versions. Check version compatibility before using.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

JavaScript — Reserved Keywords

Or Copy Link

CONTENTS
Scroll to Top