Last modified: Jul 22, 2026

JS Reserved Words as Variables Guide

JavaScript has a list of reserved words. These words are part of the language itself. You cannot use them as variable names. This is a common mistake for beginners. It causes a syntax error. Your code will not run.

Reserved words include keywords like if, else, for, while, function, and return. They also include future reserved words like class and enum. Using them as variables breaks the language rules.

Why Reserved Words Matter

JavaScript uses reserved words for its own syntax. They define control structures, loops, and functions. If you use them as variables, the interpreter gets confused. It cannot tell if you mean the keyword or your variable.

For example, you cannot name a variable if. The word if is used for conditional statements. Trying to assign a value to it will throw an error.

Understanding this helps you write cleaner code. It also prevents hard-to-find bugs. Always check your variable names against the reserved list.

List of Common Reserved Words

Here are some reserved words you should avoid:

  • if
  • else
  • for
  • while
  • function
  • return
  • var
  • let
  • const
  • class
  • enum
  • import
  • export
  • this
  • super

This list is not complete. The ECMAScript specification defines many more. You can find the full list in the official documentation.

What Happens When You Use a Reserved Word

When you try to use a reserved word as a variable, JavaScript throws an error. The error message varies by browser. Usually, it says "Unexpected token" or "SyntaxError".

Here is an example:

// This will cause an error
let if = 5;  // SyntaxError: Unexpected token 'if'
console.log(if);

This code will not run. The word if is reserved. You cannot assign a value to it.

Another example with return:

// This will also cause an error
const return = "hello";  // SyntaxError: Unexpected token 'return'

Both examples show the same problem. The interpreter sees the keyword and expects its syntax, not a variable assignment.

How to Fix Reserved Word Errors

Fixing this error is simple. Rename your variable. Use a different word that describes the same thing. Add a prefix or suffix to make it unique.

For example, instead of if, use ifCondition or isTrue. Instead of return, use returnValue or result.

Here is a corrected version:

// Correct variable names
let ifCondition = 5;
console.log(ifCondition);  // Output: 5

const returnValue = "hello";
console.log(returnValue);  // Output: hello

Now the code works. The names are descriptive and follow JavaScript variable naming rules. This is a good practice for all your variables.

Reserved Words in Strict Mode

JavaScript's strict mode adds more reserved words. These include implements, interface, package, private, protected, and public. They are reserved for future use.

In strict mode, you cannot use these words as variable names. Even if they are not used yet, the language reserves them. This prevents future compatibility issues.

Example in strict mode:

"use strict";

// This will cause an error in strict mode
let private = 10;  // SyntaxError: Unexpected strict mode reserved word

To fix this, rename the variable:

"use strict";

// Correct variable name
let privateValue = 10;
console.log(privateValue);  // Output: 10

Using Reserved Words as Object Properties

You can use reserved words as object property names. This is allowed because property names are strings. The interpreter treats them differently.

For example:

// This is allowed
let obj = {
  if: true,
  return: "yes"
};

console.log(obj.if);      // Output: true
console.log(obj.return);  // Output: yes

This works because the property name is in quotes or a string context. However, it is not recommended. It can confuse other developers. Use descriptive property names instead.

Best Practices for Variable Naming

Follow these best practices to avoid reserved word errors:

  • Use descriptive names that explain the data.
  • Avoid single-letter names except for loop counters.
  • Use camelCase for variable names (e.g., userName).
  • Check the reserved word list before naming.
  • Add prefixes like is, has, or get.

These rules help you write clean code. They also prevent conflicts with JavaScript keywords. For more tips, see our types of JavaScript variables guide.

Common Mistakes Beginners Make

Beginners often use reserved words without knowing. Here are some common examples:

  • Using name as a variable (it is a property of window).
  • Using length as a variable (it is a property of arrays).
  • Using undefined as a variable (it is a global value).

These are not strictly reserved, but they can cause issues. They shadow built-in properties. This leads to unexpected behavior.

Example with name:

// This can cause issues
let name = "John";
console.log(name);  // Output: John (but may conflict with window.name)

To avoid this, use more specific names like userName or firstName.

How to Check If a Word Is Reserved

You can check if a word is reserved using a simple test. Try to use it as a variable in your browser's console. If it throws an error, it is reserved.

Alternatively, use an online list. The MDN Web Docs has a complete list of reserved words. You can also use a linter like ESLint. It will warn you about reserved word usage.

Here is a quick test:

// Test in browser console
let test = "hello";  // Works
let class = "world"; // SyntaxError

Conclusion

JavaScript reserved words are keywords you cannot use as variables. They are part of the language syntax. Using them causes syntax errors. Always check your variable names against the reserved list.

Fix errors by renaming your variables. Use descriptive and unique names. Follow best practices for naming. This keeps your code clean and error-free.

For more on variable behavior, read our JavaScript variable scope explained guide. It covers how variables work in different contexts. Also, check out JavaScript variable shadowing to avoid naming conflicts.

Remember, good variable names make your code readable. They help you and others understand your logic. Avoid reserved words and write better JavaScript today.