Obfuscation

reserved words

Definition: Obfuscation-related term: reserved words.

Overview

In JavaScript and related languages, reserved words are identifiers that are set aside by the language specification and cannot be used as variable names, function names, or property names. These words are reserved for the language’s own syntax and semantics, and attempting to use them as identifiers results in a syntax error. Reserved words are part of the language grammar and are fundamental to how code is parsed and interpreted.

Reserved words are particularly relevant in contexts involving obfuscation, where developers may attempt to rename or obscure identifiers to make code harder to read or reverse-engineer. However, because reserved words are part of the language specification, they remain fixed and cannot be renamed or shadowed. Understanding reserved words is essential for developers working with obfuscation tools, code transformation, or dynamic code generation, as misusing them can lead to runtime errors or unexpected behavior.

reserved words developer glossary illustration

Why It Matters

Reserved words are critical in JavaScript because they define the language’s syntax. When a developer uses a reserved word as an identifier, the parser will fail, causing the script to halt execution. This is especially important in obfuscation, where identifiers may be renamed to obscure code intent. If an obfuscator incorrectly replaces a reserved word with a custom identifier, the resulting code will not execute.

Additionally, reserved words help maintain code clarity and prevent ambiguity. They ensure that the language's core constructs are always accessible by their correct names, reducing the chance of misinterpretation or misuse. For example, using function as a variable name would break the language's ability to define functions, and thus is not allowed.

How It Works

Reserved words are part of the JavaScript language grammar and are enforced by the parser at compile time. The JavaScript engine parses the source code and identifies these reserved identifiers, ensuring they are not used as identifiers in variable or function declarations. Reserved words are not just keywords; they also include future reserved words and strict mode reserved words, which may be used in certain contexts but not in others.

  • Reserved words are part of the language specification and are enforced by the JavaScript engine during parsing.
  • Reserved words cannot be used as identifiers for variables, functions, or properties.
  • Some reserved words are only reserved in strict mode, which is a way to opt into a stricter parsing and error handling.
  • Reserved words are also part of the ECMAScript standard and are consistent across all compliant JavaScript engines.
  • Reserved words can be used as property names in object literals, but not as identifiers in declarations or expressions.

Quick Reference

ItemPurposeNotes
functionDefines a functionCannot be used as a variable name
varDeclares a variableReserved in all contexts
ifConditional statementCannot be used as identifier
returnExits a functionReserved in function scope
forLoop constructCannot be used as identifier

Basic Example

This example shows a simple use of a reserved word in JavaScript. Attempting to use a reserved word like function as a variable name will result in a syntax error.

function example() {
  var function = "reserved";
  return function;
}

The line var function = "reserved"; causes a syntax error because function is a reserved word. The parser cannot interpret this as a valid declaration.

Production Example

In production environments, developers often use tools that rename identifiers to obscure code. A well-designed obfuscator must be aware of reserved words to prevent breaking code. This example shows how a function name should be protected from being replaced with a reserved word.

function processUserInput() {
  var input = "user_data";
  return input;
}

// Obfuscator should avoid renaming processUserInput to 'function' or 'var'

This version ensures that identifiers are not replaced with reserved words, maintaining code integrity. It is suitable for production because it avoids reserved word conflicts and allows the obfuscator to safely rename other identifiers without causing syntax errors.

Common Mistakes

  • Using reserved words as variable or function names leads to syntax errors and runtime failures.
  • Attempting to rename reserved words during obfuscation results in invalid code and breaks execution.
  • Confusing reserved words with keywords or identifiers can lead to subtle bugs in code generation.
  • Not accounting for strict mode reserved words in obfuscation tools can cause issues in strict mode environments.
  • Overlooking the difference between reserved words and identifiers in dynamic code generation can cause unexpected behavior.

Security And Production Notes

  • Reserved words are part of the JavaScript specification and are enforced by all compliant engines.
  • Incorrect handling of reserved words in obfuscation can lead to broken or non-executable code.
  • Reserved words must not be used as identifiers in any context to ensure compatibility across all environments.
  • Strict mode reserved words must be respected in environments where strict mode is enabled.
  • Obfuscators must maintain a list of reserved words to avoid renaming identifiers to reserved words.

Related Concepts

Reserved words are closely related to several core JavaScript concepts:

  • Keywords: Reserved words are a subset of keywords. Keywords are used to define syntax and control flow in JavaScript.
  • Identifiers: Identifiers are names used for variables, functions, and properties. Reserved words are a special class of identifiers that are forbidden.
  • Obfuscation: Obfuscation tools must be aware of reserved words to prevent renaming identifiers to reserved words.
  • Strict Mode: Some reserved words are only reserved in strict mode, affecting how code is parsed and interpreted.
  • Parser: The JavaScript parser enforces reserved word usage, making it a core part of code validation.

Further Reading

Continue Exploring

More Obfuscation Terms

Browse the full topic index or move directly into related glossary entries.