JavaScript Reserved Keywords

Words you cannot use as identifiers in JavaScript

🚫 What are Reserved Keywords?

Reserved keywords are words that JavaScript has set aside for special purposes. You cannot use these words as variable names, function names, or any other identifiers in your code. They are "reserved" for the language itself.


// ❌ This will cause an error
let if = "hello";        // 'if' is reserved
let function = 123;      // 'function' is reserved

// ✅ This is correct
let greeting = "hello";  // 'greeting' is not reserved
let number = 123;        // 'number' is not reserved
                                    

Error Message:

SyntaxError: Unexpected token 'if'

Why Reserved Keywords Matter

⚠️

Prevent Errors

Avoid syntax errors in your code

// ❌ Error
let class = "Math";
🔒

Language Integrity

Keep JavaScript keywords functional

// ✅ Correct
let className = "Math";
📚

Future Compatibility

Some words reserved for future use

// ❌ Avoid
let enum = [1, 2, 3];

Code Clarity

Makes code more readable

// ✅ Clear
let userList = [1, 2, 3];

🔹 Current Reserved Keywords

These keywords are actively used in JavaScript and cannot be used as identifiers:

🔴 Strictly Reserved Keywords:

break case catch class const continue debugger default delete do else export extends finally for function if import in instanceof let new return super switch this throw try typeof var void while with yield
// ❌ These will all cause errors:
let break = "stop";           // SyntaxError
let class = "MyClass";        // SyntaxError  
let function = "myFunc";      // SyntaxError
let return = "value";         // SyntaxError

// ✅ Use alternative names instead:
let breakPoint = "stop";      // ✅ Good
let className = "MyClass";    // ✅ Good
let functionName = "myFunc";  // ✅ Good
let returnValue = "value";    // ✅ Good

🔹 Future Reserved Keywords

These words are reserved for potential future use in JavaScript:

🟡 Future Reserved Keywords:

enum implements interface package private protected public static await abstract boolean byte char double final float goto int long native short synchronized throws transient volatile
// ❌ Avoid using these (may cause issues in strict mode):
let enum = ["red", "green", "blue"];     // Avoid
let interface = "user interface";        // Avoid
let private = "secret data";             // Avoid

// ✅ Use descriptive alternatives:
let colorEnum = ["red", "green", "blue"]; // ✅ Good
let userInterface = "user interface";     // ✅ Good
let privateData = "secret data";          // ✅ Good

🔹 Contextual Keywords

Some words are reserved only in specific contexts:

🔸 Strict Mode Keywords

// In strict mode, these are reserved:
"use strict";

// ❌ These cause errors in strict mode:
let arguments = "function args";  // Error in strict mode
let eval = "evaluation";          // Error in strict mode

// ✅ Safe to use in non-strict mode, but avoid anyway:
function regularMode() {
    // Not in strict mode - but still avoid these names
    let arguments = "args"; // Works but confusing
}

🔸 Module Keywords

// In modules, these have special meaning:
import { something } from './module.js';  // 'import' reserved
export default myFunction;               // 'export' reserved

// ❌ Don't use as variable names in modules:
let import = "bring in";    // Confusing
let export = "send out";    // Confusing

🔹 Common Mistakes and Solutions

Learn from common mistakes when dealing with reserved keywords:

🔸 Variable Naming Mistakes

// ❌ Common mistakes:
let new = "fresh";           // 'new' is reserved
let delete = "remove";       // 'delete' is reserved
let typeof = "string";       // 'typeof' is reserved

// ✅ Better alternatives:
let newItem = "fresh";       // Clear and descriptive
let deleteAction = "remove"; // Describes the action
let dataType = "string";     // Describes what it represents

🔸 Function Naming Mistakes

// ❌ Avoid these function names:
function if() { }            // 'if' is reserved
function for() { }           // 'for' is reserved
function while() { }         // 'while' is reserved

// ✅ Use descriptive function names:
function checkCondition() { }    // Clear purpose
function iterateItems() { }      // Describes action
function waitForCondition() { }  // Explains behavior

🔹 Best Practices for Naming

Follow these guidelines to avoid reserved keyword conflicts:

✅ Naming Best Practices:

  • Use descriptive names: userName instead of user
  • Add context: userClass instead of class
  • Use camelCase: firstName , lastName
  • Avoid abbreviations: functionName instead of funcName
  • Be specific: calculateTotal instead of calc
// ✅ Good naming examples:
let userAge = 25;                    // Clear and specific
let isUserActive = true;             // Boolean with 'is' prefix
let calculateUserScore = function() { // Descriptive function name
    return userAge * 2;
};

let userPreferences = {              // Object with clear purpose
    theme: "dark",
    language: "en"
};

// Function with clear action
function validateUserInput(input) {
    return input && input.length > 0;
}

console.log("User score: " + calculateUserScore());

Console Output:

User score: 50

🔹 Complete Reserved Keywords Reference

Quick reference of all reserved keywords organized by category:

📋 Complete List:

🔴 Current Keywords (Cannot Use):

break, case, catch, class, const, continue, debugger, default, delete, do, else, export, extends, finally, for, function, if, import, in, instanceof, let, new, return, super, switch, this, throw, try, typeof, var, void, while, with, yield

🟡 Future Reserved (Avoid Using):

abstract, await, boolean, byte, char, double, enum, final, float, goto, implements, int, interface, long, native, package, private, protected, public, short, static, synchronized, throws, transient, volatile

🧠 Test Your Knowledge

Which of these is NOT a reserved keyword in JavaScript?